Chris
Chris

Reputation: 8091

result for variable not as expected

ok, this is my updated question because still the variable will be shown false in another function.

I changed now my code as Asperi proposed (thank you Asperi)...looks better now, but...

still in my function updatePivotForTextNode() the animationType is .none!?

Output debugger:

makeuiview flyThrough // good

2020-02-20 14:30:00.287089+0100 EasyTest[3837:134164] Metal API Validation Enabled

dy= 7.3564453

update pivot none // not good

updateui flyThrough // good

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()
    }
}

Upvotes: 1

Views: 118

Answers (1)

Asperi
Asperi

Reputation: 257493

Just don't initialise it before init

final class SceneTextViewWrapper : UIViewRepresentable {

    @State var animationType : SceneTextView.AnimationType // << only declaration

Upvotes: 2

Related Questions