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

76 views Asked by At

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...

2

There are 2 answers

0
iOSDeveloper On BEST ANSWER

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

0
ZAY On

Here is an example code snippet how to work with a SCNTransaction. This example has a completion handler. If you don't need this, you can delete the section for the handler.

// Scene Transaction with completion handler example
SCNTransaction.begin()
SCNTransaction.animationDuration = 6.0

// The completion handling section
SCNTransaction.completionBlock = {
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 1.0
    explosion.particleColor = UIColor.blue
    SCNTransaction.commit()
}

cameraNode.camera?.focalLength = 50
SCNTransaction.commit()



// Scene Transaction without completion handler
SCNTransaction.begin()
SCNTransaction.animationDuration = 6.0
cameraNode.camera?.focalLength = 50
SCNTransaction.commit()

As you can see here, we are modifying the focalLength of a SCNCamera. Try to make a similar adaption for your transform value.