mike_t
mike_t

Reputation: 2691

scenekit - animate SCNNode.transform

Apple docs state that SCNNode.transform is animatable. What is the best way to animate following transformation?

cameraOrbit.transform = SCNMatrix4Mult(cameraOrbit.transform,SCNMatrix4MakeRotation(angle,  x, y, 0.0))

Upvotes: 0

Views: 1781

Answers (2)

P. Stern
P. Stern

Reputation: 361

Maybe something changed in newer versions of Swift, but @mike_t's answer results in cameraOrbit returning to its original transform when the animation completes. Based on this blog post you need to do something like this:

let originalTransform = cameraOrbit.transform
cameraOrbit.transform = SCNMatrix4Rotate(cameraOrbit.transform, angle, x, y, 0.0)
let animation = CABasicAnimation(keyPath: "transform")
animation.fromValue = originalTransform
animation.duration = 0.5
cameraOrbit.addAnimation(animation, forKey: nil)

Apparently, the animation is only done on the presentation layer. When the animation completes, the presentation layer returns to showing the unchanged model layer. By changing the model layer explicitly (second line), and starting the animation from the original transform, the presentation layer shows the desired rotation and finishes by showing the already transformed model layer.

Upvotes: 0

mike_t
mike_t

Reputation: 2691

Based on @rickster guidance, this is the working code in case somebody runs into similar issue -

 let animation = CABasicAnimation(keyPath: "transform")
 animation.fromValue = cameraOrbit.transform
 animation.toValue = SCNMatrix4Mult(cameraOrbit.transform,SCNMatrix4MakeRotation(angle,  x, y, 0.0))
 animation.duration = 0.5
 cameraOrbit.addAnimation(animation, forKey: nil)

Upvotes: 3

Related Questions