scenekitcabasicanimationscnnode

How to Fix SCNNode Transform Problem with CABasicAnimation in SceneKit in Swift?


I have a problem. I rotate it but SCNNode returns to its starting position.

Look at the gif please: https://i.ibb.co/X2kz6fK/ezgif-5-d0417c4423.gif

My code:

enter image description here

Thanks everyone...


Solution

  • The code blocks below solved my problem.

        let rotation = SCNMatrix4MakeRotation(-Float(angle.degreesToRadians), 0, 1, 0)
        let newTransform = SCNMatrix4Mult(cubeNode.transform, rotation)
        let animation = CABasicAnimation(keyPath: "transform")
        animation.toValue = scene.rootNode.convertTransform(newTransform, from: nil)
        animation.duration = 1.5
        animation.fillMode = .forwards
        animation.isRemovedOnCompletion = false
        cubeNode.addAnimation(animation, forKey: nil)
    

    Thanks