Home > Blockchain >  How to animate SCNNode.scale using SCNTransaction
How to animate SCNNode.scale using SCNTransaction

Time:11-25

let balloon: SCNNode

SCNTransaction.begin()
SCNTransaction.animationDuration = 4
SCNTransaction.animationTimingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
balloon.scale.x = 2.28
balloon.scale.y = 2.28
balloon.scale.z = 2.28
// or balloon.scale = SCNVector3(2.28, 2.28, 2.28)
SCNTransaction.commit()

The above does not animate. Docs say .scale is animatable however the new scale is applied immediately.

I have the same issue with the material's diffuse color.

The following works:

balloon.runAction(SCNAction.scale(to: 2.28, duration: 4), forKey: "scale")
DispatchQueue.main.asyncAfter(deadline: .now()   1) { [self] in
    balloon.removeAction(forKey: "scale")
}

however sometimes I prefer SCNTransaction because an animation can be replaced or stopped just by setting a new value (directly or via another animation) instead of having to explicitly stop it like with .removeAnimation, and also because of its main function which is atomicity.

What am I missing?

CodePudding user response:

It might be the physics body of your node that prevents the SCNTransaction to run as expected. This is, as I remeber, by design. Physics bodies cannot be scaled. It could be an option to temporary remove the physics body and re-apply it after the SCNTransaction has completed.

  • Related