简体   繁体   English

有没有办法在 Swift 中反转动画?

[英]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?有没有办法在 Swift 中反转动画? I have a function that has some basic animations within a SCNTransaction and I have a button that activates that animation.我有一个在 SCNTransaction 中有一些基本动画的函数,我有一个按钮可以激活该动画。

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.我查看了文档,但似乎没有 .reversed 方法。 How can I make this animation just as smooth but in reverse?我怎样才能让这个动画同样流畅但相反?

Use SCNAction to add reverse animation.使用 SCNAction 添加反向动画。

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.我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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