【发布时间】:2021-12-21 19:31:37
【问题描述】:
我正在尝试使用 CAKeyframeAnimation 为 SCNnode 设置动画,我能够毫无问题地更改对象的位置,但无法为 EulerAngle 属性的更改设置动画。
下面的代码改变了节点的位置。
func animatePlaneKey(nodeToAnimate: SCNNode){
let pos = nodeToAnimate.position
let animation = CAKeyframeAnimation(keyPath: "position")
let pos1 = SCNVector3(pos.x, pos.y, pos.z)
let pos2 = SCNVector3(pos.x + 1 , pos.y, pos.z)
let pos3 = SCNVector3(pos.x + 1 , pos.y, pos.z + 1)
animation.values = [pos1,pos2, pos3]
animation.keyTimes = [0,0.5,1]
animation.calculationMode = .linear
animation.duration = 10
animation.repeatCount = 1
animation.isAdditive = true
nodeToAnimate.addAnimation(animation, forKey: "position")
}
下面的应该更改 eulerAngles 属性但不起作用。 知道为什么我不能为旋转设置动画吗?
func animatePlaneKey(nodeToAnimate: SCNNode){
let animation2 = CAKeyframeAnimation(keyPath: "rotation")
let angles = nodeToAnimate.eulerAngles
let rot0 = SCNVector3(angles.x, angles.y, angles.z)
let rot1 = SCNVector3(angles.x, angles.y + Float(deg2rad(45)) , angles.z)
animation2.values = [rot0, rot1]
animation2.keyTimes = [0, 1]
animation2.duration = 2
animation2.repeatCount = .infinity
animation2.isAdditive = true
nodeToAnimate.addAnimation(animation2, forKey: "rotation")
}
感谢您的帮助。
【问题讨论】:
-
我推荐:SCNAction.rotate(by: around, duration) - i.Ex: let rotate = SCNAction.rotate(by: CGFloat.pi*2, around: SCNVector3(0,1,0 ),持续时间:5.0) - 这会将 Y 轴上的节点旋转 360 度。您可以重复 SCNAction - 也可以永远重复。
-
SCNNode 旋转属性是一个 SCNVector4。将旋转更改为 SCNVector4,或将 keyPath 更改为“eulerAngles”。
标签: swift animation scenekit arkit