簡體   English   中英

如何在不同的視圖中使用 SwiftUI?

[英]How to use SwiftUI with different views?

我在SCNCylinder中有SCNScene ,在其下方的 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'”。

您需要在ContentView聲明一個@State並在Button操作中設置該屬性的值。

然后,您需要將SceneKitViewangle屬性聲明為@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)
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM