简体   繁体   English

如何在不同的视图中使用 SwiftUI?

[英]How to use SwiftUI with different views?

I have SCNCylinder in a SCNScene and a button in a SwiftUI frame below it.我在SCNCylinder中有SCNScene ,在其下方的 SwiftUI 框架中有一个按钮。 Whenever the button is pressed the cylinder is expected to rotate by 90°.每当按下按钮时,圆柱体预计会旋转 90°。 I am not getting the expected result with my code.我的代码没有得到预期的结果。

//SwiftUI 

struct ContentView: View {

  var body: some View {

    VStack{ 

        Button(action: {
            // What to perform

             let rotationangle = 180.0
        }) {
            // How the button looks like

            Text("90°")
                .frame(width: 100, height: 100)
                .position(x: 225, y: 500)

            }

            SceneKitView()
                .frame(width: 300, height: 300)
                .position(x: 0, y: 200)


      }
   }
}

//SceneKit 

struct SceneKitView: UIViewRepresentable {
    func makeUIView(context:     
    UIViewRepresentableContext<SceneKitView>) -> SCNView {
     //Scene properties 

       let sceneView = SCNView()
       sceneView.scene = SCNScene()
       sceneView.allowsCameraControl = true
       sceneView.autoenablesDefaultLighting = true
       sceneView.backgroundColor = UIColor.white
       sceneView.frame = CGRect(x: 0, y: 10, width: 0, height: 1)

    //Cylinder properties 

    let cylinder  = SCNCylinder(radius: 0.02, height: 2.0)
    let cylindernode  = SCNNode(geometry: cylinder)
    cylindernode.position = SCNVector3(x: 0, y: 0, z: 0)
    cylinder.firstMaterial?.diffuse.contents = UIColor.green

    //Pivot and rotation of the cylinder    

    func degreesToRadians(_ degrees: Float) -> CGFloat {
        return CGFloat(degrees * .pi / 180)
    }

    cylindernode.pivot = SCNMatrix4MakeTranslation(0, -1, 0)

    //Line with an error: Use of unresolved identifier ‘rotationangle’
    let rotation = SCNAction.rotate(by: degreesToRadians(rotationangle), around: SCNVector3 (1, 0, 0), duration: 5)


    sceneView.scene?.rootNode.addChildNode(cylindernode)


    cylindernode.runAction(rotation)


    return sceneView
}

func updateUIView(_ uiView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {

}

typealias UIViewType = SCNView
}

I am having a error saying “ Use of unresolved identifier 'rotationangle' “ during using 'rotationangle' in SCNAction.在 SCNAction 中使用 'rotationangle' 期间,我收到一条错误消息,提示“使用未解析的标识符 'rotationangle'”。

You need to declare a @State in your ContentView and set the value of the property in your Button action.您需要在ContentView声明一个@State并在Button操作中设置该属性的值。

You then need to declare an angle property in your SceneKitView as a @Binding and use that value for it to work properly.然后,您需要将SceneKitViewangle属性声明为@Binding并使用该值使其正常工作。

I haven't yet tested this out.我还没有测试过这个。 Keep in mind that you don't have cone declared anywhere so I'm not sure what that is.请记住,您没有在任何地方声明cone所以我不确定那是什么。

import SwiftUI
import SceneKit

struct ContentView: View {
    @State var rotationAngle: Float = 0.0

    var body: some View {
        VStack {
            Button(action: {
                // What to perform
                self.rotationAngle = 180.0
            }) {
                // How the button looks like
                Text("90°")
                    .frame(width: 100, height: 100)
                    .position(x: 225, y: 500)
            }

            SceneKitView(angle: self.$rotationAngle)
                .frame(width: 300, height: 300)
                .position(x: 0, y: 200)
        }
    }
}

//SceneKit

struct SceneKitView: UIViewRepresentable {
    @Binding var angle: Float

    func degreesToRadians(_ degrees: Float) -> CGFloat {
        print(degrees)
        return CGFloat(degrees * .pi / 180)
    }

    func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
        // Scene properties
        let sceneView = SCNView()

        sceneView.scene = SCNScene()
        sceneView.allowsCameraControl = true
        sceneView.autoenablesDefaultLighting = true
        sceneView.backgroundColor = UIColor.white
        sceneView.frame = CGRect(x: 0, y: 10, width: 0, height: 1)

        return sceneView
    }

    func updateUIView (_ sceneView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {
        if self.angle > 0 {
            // Cylinder properties
            let cylinder  = SCNCylinder(radius: 0.02, height: 2.0)
            let cylindernode = SCNNode(geometry: cylinder)
            cylindernode.position = SCNVector3(x: 0, y: 0, z: 0)
            // cone.firstMaterial?.diffuse.contents = UIColor.green

            // Pivot and rotation of the cylinder

            cylindernode.pivot = SCNMatrix4MakeTranslation(0, -1, 0)
            sceneView.scene?.rootNode.addChildNode(cylindernode)

            //Line with an error: Use of unresolved identifier ‘rotationangle’
            let rotation = SCNAction.rotate(by: self.degreesToRadians(self.angle), around: SCNVector3 (1, 0, 0), duration: 5)

            cylindernode.runAction(rotation)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM