Search code examples
scenekitarkit

Stop Scenkit animation in the last frame


I played all animation together in sceneKitand it should play once and stop it in the last frame - don't come back to the initial position

private func playAnimation(with animation: ARAnimation) {
    for object in virtualObjectLoader.loadedObjects {
        
        var animationPlayer: SCNAnimationPlayer! = nil
        
        object.enumerateChildNodes { (child, stop) in
            if !child.animationKeys.isEmpty, object.hash == animation.objectSystemHash {
                
                for animation in animation.animationKeys {
                    animationPlayer = child.animationPlayer(forKey: animation)
                    animationPlayer.speed = 1
                    animationPlayer.animation.repeatCount = 0                                            
                    animationPlayer.play()
                                         
                }
            }
        }
    }
    reserveAnimationButton.isHidden = false
    playedAnimation = animation
}

and later I want to reverse it

    private func reverseAnimation() {
        guard let animation = playedAnimation else { return }
        
        for object in virtualObjectLoader.loadedObjects {
            
            var animationPlayer: SCNAnimationPlayer! = nil
            
            object.enumerateChildNodes { (child, stop) in
                if !child.animationKeys.isEmpty, object.hash == animation.objectSystemHash {
                    
                    for animation in animation.animationKeys {
                        animationPlayer = child.animationPlayer(forKey: animation)
                        animationPlayer.speed = -1
                        animationPlayer.animation.repeatCount = 1
                        animationPlayer?.animation.isRemovedOnCompletion = false
                        animationPlayer.play()
                        
                    }
                }
            }
        }
        reserveAnimationButton.isHidden = true
    }

But when the animation played, and done, it comes back to the initial position, which make the reverse animation pointless. Could anyone help me how to manage that. I also want to do this process few times, not all animations remove after the reversing.

Thanks


Solution

  • The fillsForward property should be useful.

    It's similar to Core Animation's kCAFillModeForwards use in fillMode.