如何使SCNNode水平旋转/旋转



我正在尝试让我的SCNNode自动水平旋转。这是我到目前为止的代码:

box.scale = SCNVector3(x: 0.26, y: 0.26, z: 0.26)
box.position = SCNVector3(0.15, 3.85, -3)

如何使这个盒子自动水平旋转?另外,有没有办法在启用用户交互的情况下实现它?谢谢!

更新:如果我听起来不那么清楚,这就是我的意思:你知道当你用手指旋转篮球时以及它是如何水平旋转的吗?这就是我想要达到的效果!谢谢!

你可以

  • 使用SCNAction类,例如SCNAction.rotate(by:around:duration:).这样做的优点是您可以链接多个动画或永远重复它们,

编辑:

let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 5))
box.runAction(action)
  • 使用SCNTransaction

您可以像这样更改转换

let oldTransform = box.transform
let rotation = SCNMatrix4MakeRotation(axis.x, axis.y, axis.z angle)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
box.transform = SCNMatrix4Mult(rotation, oldTransform)
SCNTransaction.commit()

要使用用户输入,例如,您可以使用手势识别器更改旋转角度或持续时间

使用SceneView 的 SwiftUI 使用一些稍新的语法更新了接受的答案。

在iOS14/iOS15上运行良好

let oldTransform = cubeNode.transform
let rotation = SCNMatrix4MakeRotation(GLKMathDegreesToRadians(90), 0, 1, 0)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
cubeNode.transform = SCNMatrix4Mult(rotation, oldTransform)
SCNTransaction.commit()

最新更新