Ios 如何围绕多个轴以不同角度旋转SCN节点?

Ios 如何围绕多个轴以不同角度旋转SCN节点?,ios,3d,rotation,swiftui,scenekit,Ios,3d,Rotation,Swiftui,Scenekit,我在SwiftUI的一个框架内的“SCNView”中有一个“SCNCylinder”。我的目标是围绕不同的轴以不同的角度旋转圆柱体。我有两个(类)按钮,可以从用户处获取180°或90°的输入。我想在不同的轴上旋转它们。我想将圆柱体绕“SCInvector3(1,0,0)”旋转180°,绕“SCInvector3(0,0,1)”旋转90°。我已经做了一个能够做到这一点的操场。真正的问题是用同样的方法进行多次旋转。假设用户先敲打180°,然后敲打90°,我希望圆柱体也这样做。但目前它的旋转角度为18

我在SwiftUI的一个框架内的“SCNView”中有一个“SCNCylinder”。我的目标是围绕不同的轴以不同的角度旋转圆柱体。我有两个(类)按钮,可以从用户处获取180°或90°的输入。我想在不同的轴上旋转它们。我想将圆柱体绕“SCInvector3(1,0,0)”旋转180°,绕“SCInvector3(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)
}
struct ContentView:View{
@状态变量旋转角度:角度=.degrees(0)
@状态变量rotationAngle2:角度=.degrees(0)
var body:一些观点{
VStack{
HStack{
文本(“180°”).ontaps手势{
自转角度=.度(180)
}
分隔器()
文本(“90°”).ontaps手势{
自转角度2=.度(90)
}
}     
SceneKitView(半径:0.02,高度:2,角度:$rotationAngle,角度2:$rotationAngle2)
.位置(x:225.0,y:175)
.框架(宽度:300,高度:300,对齐:。中心)
}  
}
}
结构SceneKitView:UIViewRepresentable{
@绑定变量角度:角度
@绑定变量角度2:角度
让圆柱体节点:SCNNode
初始(半径:CGFloat,高度:CGFloat,角度:绑定,角度2:绑定){
让圆柱体=圆柱体(半径:半径,高度:高度)
圆柱体.firstMaterial?.diffuse.contents=UIColor.green
self.cylindernode=SCNNode(几何图形:圆柱体)
self.cylindernode.position=SCInvector3(0,0,0)
cylindernode.pivot=SCNMatrix4MakeTranslation(0,-1,0)
自身角度=角度
自身角度2=角度2
}
func makeUIView(上下文:UIViewRepresentableContext)->SCNView{
让sceneView=SCNView()
sceneView.scene=SCNScene()
sceneView.autoenablesDefaultLighting=true
sceneView.allowsCameraControl=true
sceneView.scene?.rootNode.addChildNode(cylindernode)
返回场景视图
}
func UpdateUI视图(sceneView:SCNView,context:UIViewRepresentableContext){
让旋转=SCNAction.rotate(按:CGFloat(角度弧度),围绕:scinvector3(1,0,0),持续时间:3)
让rotation2=SCNAction.rotation(通过:CGFloat(角度2.弧度),围绕:scinvector3(0,0,1),持续时间:3)
打印(角度、度数)
打印(角度2度)
cylindernode.runAction(旋转)
cylindernode.runAction(旋转2)
}
}


请帮我解决这个问题

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

e、 g

1.)如果希望动画并行运行

cylindernode.runAction(SCNAction.group([rotation, rotation2]))
2.)如果希望动画按顺序运行

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

我不确定你的问题是否正确。旋转顺序不是问题所在。真正的问题是,对于用户给出的第二个输入,我得到了随机旋转。你确定结果是随机的吗?我相信是的。至少我找不到任何线索为什么它会这样旋转。