результат для переменной не такой, как ожидалось - PullRequest
1 голос
/ 20 февраля 2020

хорошо, это мой обновленный вопрос, потому что переменная будет отображаться как ложная в другой функции.

Я изменил свой код, как предложил Аспери (спасибо Аспери) ... теперь выглядит лучше, но. ..

все еще в моей функции updatePivotForTextNode () animationType имеет значение .none!?

Отладчик вывода:

makeuiview flyThrough // good

2020- 02-20 14: 30: 00.287089 + 0100 EasyTest [3837: 134164] Проверка API по металлу включена

dy = 7.3564453

обновить сводку нет // не очень хорошо

updateui flyThrough // хорошо

import SwiftUI
import SceneKit

class SceneTextView: SCNView {

    enum AnimationType {
        case none
        case wiggleHorizontal
        case flyThrough
    }

    var text: String {
        didSet {
            (textNode?.geometry as! SCNText).string = text
            updatePivotForTextNode()
        }
    }

    let cameraNode = SCNNode()

    var font : UIFont = UIFont(name: "Verdana", size: 12)!
    var color: UIColor = .orange
    var lightIntensity : CGFloat = 30000
    var cameraPosition : SCNVector3 = SCNVector3(2,0,40)
    var spotlightPosition : SCNVector3 = SCNVector3(6,2,25)
    var animationType : AnimationType = .none

    var textNode : SCNNode? = nil

    var justOnce = true

    var animatingAngle : CGFloat = 0

    override init(frame: CGRect, options: [String : Any]? = nil) {

        self.text = ""
        self.animatingAngle = CGFloat.pi * 2 / 30
        super.init(frame: frame, options: options)
    }

    init(frame: CGRect, animatingAngle: CGFloat = CGFloat.pi * 2 / 30,  options: [String : Any]? = nil) {

        self.text = ""
        self.animatingAngle = animatingAngle
        super.init(frame: frame, options: options)
    }

    func updatePivotForTextNode() {

        let boundingBox = textNode!.boundingBox

        let max = boundingBox.max
        let min = boundingBox.min
        let dx = min.x + 0.5 * (max.x - min.x)
        let dy = animationType == .flyThrough ? min.y + 0.5 * (max.y - min.y) * 2 : min.y + 0.5 * (max.y - min.y)
        // weird result here
        print("dy=",dy)
        print("update pivot", self.animationType)
        let dz = min.z + 0.5 * (max.z - min.z)


    }

    init(frame: CGRect, font: UIFont, color: UIColor, lightIntensity: CGFloat = 30000, lightPosition: SCNVector3 = SCNVector3(6,2,25), cameraPosition: SCNVector3 = SCNVector3(2,0,40), text: String, animationType: AnimationType = .none) {

        self.text = text
        super.init(frame: frame) // !! < here

        self.font = font
        self.color = color
        self.lightIntensity = lightIntensity
        self.spotlightPosition = lightPosition
        self.animationType = animationType


        backgroundColor = UIColor.clear

        scene = SCNScene()

        let textGeometry = SCNText(string: "Bla", extrusionDepth: 4)
        textGeometry.firstMaterial?.isDoubleSided = true
        textNode = SCNNode(geometry: textGeometry)
        scene?.rootNode.addChildNode(textNode!)

        updatePivotForTextNode()

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

final class SceneTextViewWrapper : UIViewRepresentable {

    @State var animationType : SceneTextView.AnimationType

    init(animationType: SceneTextView.AnimationType ) {
        self.animationType = animationType
    }

    func makeUIView(context: Context) -> SceneTextView {
        print("makeuiview", animationType)
        return SceneTextView(frame: CGRect(x: 0, y: 0, width: 200, height: 200),
                             font: UIFont.boldSystemFont(ofSize: 3),
                             color: .blue,
                             lightIntensity: 8000,
                             lightPosition: SCNVector3(0,0,0),
                             cameraPosition: SCNVector3(0,0,22),
                             text: "Winner",
                             animationType: .flyThrough)
    }

    func updateUIView(_ uiView: SceneTextView, context: UIViewRepresentableContext<SceneTextViewWrapper>) {
        print("updateui", animationType)
    }
}

struct ContentView: View {

    @State var animate = true

    var body: some View {
        ZStack {
            Rectangle()
                .background(Color.black)
            SceneTextViewWrapper(animationType: .flyThrough)
                .onAppear() {

            }
        }.edgesIgnoringSafeArea(.all)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1 Ответ

1 голос
/ 20 февраля 2020

Только не инициализируйте его раньше init

final class SceneTextViewWrapper : UIViewRepresentable {

    @State var animationType : SceneTextView.AnimationType // << only declaration
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...