如何围绕多个轴旋转SCNNode?

时间:2019-12-14 10:54:23

标签: ios swift 3d swiftui scenekit

我在SceneKit的SCNCylinder中有一个SCNView。我的目标是使圆柱体分别在不同的旋转轴上旋转90°和180°。我正在使用SwiftUI输入要旋转的角度。我做了这个达到同样目的的游乐场。但是,我的错误是:“在运行该游乐场时遇到问题。请检查您的代码是否有错误。”不确定为什么会收到此错误。我相信我已经以编程方式正确地完成了所有工作。

这是我的代码:

struct ContentView: View {
@State var rotationAngle: Angle = .zero
@State var rotationAngle2: Angle = .zero
var body: some View {

    VStack{

        Text("180°").onTapGesture {
            self.rotationAngle = .degrees(180)
            self.rotationAngle2 = .degrees(0)
        }

        Divider()

        Text("90°").onTapGesture {
            self.rotationAngle = .degrees(0)
            self.rotationAngle2 = .degrees(90)
        }



        SceneKitView(radius: 0.02, height: 2, angle: $rotationAngle, angle2: $rotationAngle2)
            .position(x: 225.0, y: 175)
            .frame(width: 300, height: 300, alignment: .center)

    }
   }
 }

struct SceneKitView: UIViewRepresentable {
   @Binding var angle: Angle
   @Binding var angle2 : Angle


let cylindernode: SCNNode

init(radius: CGFloat, height: CGFloat, angle: Binding<Angle>, angle2: Binding<Angle>) {

    let cylinder = SCNCylinder(radius: radius, height: height)
    cylinder.firstMaterial?.diffuse.contents = UIColor.green
    self.cylindernode = SCNNode(geometry: cylinder)
    self.cylindernode.position = SCNVector3(0, 0, 0)
    cylindernode.pivot = SCNMatrix4MakeTranslation(0, -1, 0)
    self._angle = angle
    self._angle2 = angle2

}

func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {

    let sceneView = SCNView()
    sceneView.scene = SCNScene()
    sceneView.autoenablesDefaultLighting = true
    sceneView.allowsCameraControl = true
    sceneView.scene?.rootNode.addChildNode(cylindernode)
    return sceneView
}

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

    let rotation = SCNAction.rotate(by: CGFloat(angle.radians), around: SCNVector3(1, 0, 0), duration: 3)

    let rotation2 = SCNAction.rotate(by: CGFloat(angle2.radians), around: SCNVector3(0, 0, 1), duration: 3)

    cylindernode.runAction(rotation)
    cylindernode.runAction(rotation2)
}

}

1 个答案:

答案 0 :(得分:0)

在我添加4行(只是导入+ setLiveView)之后测试代码,当我启动它时没有错误。