0

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))
mike_t
  • 2,484
  • 2
  • 21
  • 39
  • 1
    See [this Q&A](https://stackoverflow.com/a/25674762/957768) for some scene setup help, then the [Apple docs on animation](https://developer.apple.com/documentation/scenekit/animation/animating_scenekit_content) for making it animate. – rickster Oct 05 '17 at 22:05
  • Thanks for pointing me to right direction, very helpful. – mike_t Oct 08 '17 at 17:05

2 Answers2

3

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)
mike_t
  • 2,484
  • 2
  • 21
  • 39
0

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.

P. Stern
  • 279
  • 1
  • 5