要绕世界轴旋转节点,请将节点worldTransform 与旋转矩阵相乘。
我还没有找到使用SCNAction 的解决方案,但使用SCNTransaction 非常简单。
func rotate(_ node: SCNNode, around axis: SCNVector3, by angle: CGFloat, duration: TimeInterval, completionBlock: (()->())?) {
let rotation = SCNMatrix4MakeRotation(angle, axis.x, axis.y, axis.z)
let newTransform = node.worldTransform * rotation
// Animate the transaction
SCNTransaction.begin()
// Set the duration and the completion block
SCNTransaction.animationDuration = duration
SCNTransaction.completionBlock = completionBlock
// Set the new transform
node.transform = newTransform
SCNTransaction.commit()
}
如果节点的父节点具有不同的变换,则此方法不起作用,但我们可以通过将生成的变换转换为父节点坐标空间来解决此问题。
func rotate(_ node: SCNNode, around axis: SCNVector3, by angle: CGFloat, duration: TimeInterval, completionBlock: (()->())?) {
let rotation = SCNMatrix4MakeRotation(angle, axis.x, axis.y, axis.z)
let newTransform = node.worldTransform * rotation
// Animate the transaction
SCNTransaction.begin()
// Set the duration and the completion block
SCNTransaction.animationDuration = duration
SCNTransaction.completionBlock = completionBlock
// Set the new transform
if let parent = node.parent {
node.transform = parent.convertTransform(newTransform, from: nil)
} else {
node.transform = newTransform
}
SCNTransaction.commit()
}
你可以在thisSwift Playground 试试这个。
我希望这就是你要找的。p>