【问题标题】:How to use SwiftUI with different views?如何在不同的视图中使用 SwiftUI?
【发布时间】:2020-04-04 06:12:39
【问题描述】:

我在SCNScene 中有SCNCylinder,在它下方的 SwiftUI 框架中有一个按钮。每当按下按钮时,圆柱体都会旋转 90°。我的代码没有得到预期的结果。

//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
}

在 SCNAction 中使用“rotationangle”时出现错误提示“使用未解析的标识符“rotationangle”。

【问题讨论】:

    标签: scenekit swiftui swift5


    【解决方案1】:

    您需要在您的ContentView 中声明一个@State,并在您的Button 操作中设置该属性的值。

    然后,您需要在 SceneKitView 中将 angle 属性声明为 @Binding,并使用该值使其正常工作。

    我还没有对此进行测试。请记住,您没有在任何地方声明 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)
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。但是旋转动作仍然没有等我触摸到按钮。只要我运行代码,它就会旋转。我希望它在我点击按钮后旋转。
    • 这是因为默认的rotationAngle180.0。这与您最初提出的问题确实是一个不同的问题。我修改了代码以将默认的angle 设置为0.0,然后在updateUIView(...) 方法中设置一个条件,以仅在angle 大于0.0 时运行该操作。这应该足够了。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2023-04-07
    • 2021-01-03
    相关资源
    最近更新 更多