简体   繁体   中英

Is there a way to reverse animations in Swift?

I'm currently learning so excuse my ignorance. Is there a way to reverse an animation in Swift? I have a function that has some basic animations within a SCNTransaction and I have a button that activates that animation.

func displayAnimation() {
        if let displayAnimationNode = sceneView.scene.rootNode.childNode(withName: "display", recursively: true) {

            SCNTransaction.begin()
            displayAnimationNode.position.y = 3
            displayAnimationNode.eulerAngles.z = -2
            SCNTransaction.animationDuration = 2.5
            SCNTransaction.commit()
        }
    }

I have looked through the documentation but there doesn't seem to be a .reversed method. How can I make this animation just as smooth but in reverse?

Use SCNAction to add reverse animation.

let moveY = SCNAction.moveBy(x: 0, y: 3, z: 0, duration: 2.5)
let moveZ = SCNAction.moveBy(x: 0, y: 0, z: -2, duration: 2.5)
let moveYZ = SCNAction.sequence([moveY, moveZ])

// You can use group to simultaneously perform animation
// let rotateAndHover = SCNAction.group([moveY, moveZ])

// This line of code will reverse your animation
moveYZ.reversed()    

let repeatForever = SCNAction.repeatForever(moveXZ)

runAction(repeatForever)

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM