如何围绕多个轴和不同角度旋转SCNNode?

问题描述 投票:0回答:1

我在SwiftUI的框架内的“ SCNView”中有一个“ SCNCylinder”。我的目标是使圆柱体绕不同的轴并以不同的角度旋转。我有两个(某种)按钮,它们从用户输入的角度分别为180°或90°。我想在不同的轴上旋转它们。我想将圆柱体围绕“ SCNVector3(1,0,0)”旋转180°,并围绕“ SCNVector3(0,0,1)”旋转90°。我已经做了一个可以做到这一点的游乐场。真正的问题是对同一对象进行多次旋转。假设用户先点击180°,然后再点击90°,我希望圆柱体也这样做。但是目前,它可以旋转180°,但是当用户点击90°按钮时会随机旋转。

这是我的代码:

struct ContentView: View {

@State var rotationAngle: Angle = .degrees(0)
@State var rotationAngle2: Angle = .degrees(0)
var body: some View {

    VStack{

        HStack{
        Text("180°").onTapGesture {
            self.rotationAngle = .degrees(180)

        }

        Divider()

        Text("90°").onTapGesture {
            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)

    print(angle.degrees)
    print(angle2.degrees)

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

}

请帮助我解决这个问题。

ios 3d rotation swiftui scenekit
1个回答
0
投票

我不确定如果在同一节点上两次运行Action,但是可以通过以下功能“控制”动画,会发生什么情况:

例如

1。)如果您希望动画并行运行

cylindernode.runAction(SCNAction.group([rotation, rotation2]))

2。)如果您希望动画按顺序运行

cylindernode.runAction[SCNAction.sequence([rotation, rotation2]))
© www.soinside.com 2019 - 2024. All rights reserved.