Ios 如何在swift中触发动画序列

Ios 如何在swift中触发动画序列,ios,swift,Ios,Swift,在Swift 4中,我从阵列中的dae文件加载/保存一组动画: var animations = [String: CAAnimation]() loadAnimation(withKey: "stepL", sceneName: "art.scnassets/StepL", animationIdentifier: "StepL-1") loadAnimation(withKey: "stepR", sceneName: "art.scnassets/StepR", animationIden

在Swift 4中,我从阵列中的dae文件加载/保存一组动画:

var animations = [String: CAAnimation]()
loadAnimation(withKey: "stepL", sceneName: "art.scnassets/StepL", animationIdentifier: "StepL-1")
loadAnimation(withKey: "stepR", sceneName: "art.scnassets/StepR", animationIdentifier: "StepR-1")


func loadAnimation(withKey: String, sceneName:String, animationIdentifier:String) {
    let sceneURL = Bundle.main.url(forResource: sceneName, withExtension: "dae")
    let sceneSource = SCNSceneSource(url: sceneURL!, options: nil)

    if let animationObject = sceneSource?.entryWithIdentifier(animationIdentifier, withClass: CAAnimation.self) {
        // The animation will only play once
        animationObject.repeatCount = 1
        // To create smooth transitions between animations
        animationObject.fadeInDuration = CGFloat(0.8)
        animationObject.fadeOutDuration = CGFloat(0.6)

        // Store the animation for later use
        animations[withKey] = animationObject
    }
}
稍后,我将通过将动画添加到场景来播放动画:

func playAnimation(key: String) {
    print("fire animation: " + key)
    scnView.scene?.rootNode.addAnimation(animations[key]!, forKey: key)
}
这似乎是工作时,做他们单独从触摸,但我想能够触发这些非常具体的时间序列

我尝试构建一个循环,并将它们发送到一个调度队列,列出了具体的时间安排:

        var delaytime = state.steptime!
        for _ in 0..<state.trCount!  {
            for wSide in state.walkSeq! {
                walkQ.asyncAfter(deadline: .now() + delaytime){
                    self.playAnimation(key: wSide)
                }
                delaytime += state.steptime! + 1.0
            }
        }
var delaytime=state.steptime!

对于uu0..我最终按照需要的顺序进行了一系列操作,包括等待

        var seq = [SCNAction]()

        for _ in 0..<state.trCount!{
            let act1 = SCNAction.run {_ in
                self.playAnimation(key: "stepL")
            }
            seq.append(act1)

            let act2 = SCNAction.wait(duration: state.steptime!)
            seq.append(act2)

            let act3 = SCNAction.run { _ in
                self.playAnimation(key: "stepR")
            }
            seq.append(act3)
            seq.append(act2)

            let act4 = SCNAction.run { _ in
                self.playAnimation(key: "idle")
            }
            seq.append(act4)

            let act5 = SCNAction.wait(duration: state.pausetime!)
            seq.append(act5)
        }

        let sequence = SCNAction.sequence(seq)
        scnView.scene?.rootNode.runAction(sequence, completionHandler:nil )
var-seq=[SCNAction]()
对于0中的uu。。
        var seq = [SCNAction]()

        for _ in 0..<state.trCount!{
            let act1 = SCNAction.run {_ in
                self.playAnimation(key: "stepL")
            }
            seq.append(act1)

            let act2 = SCNAction.wait(duration: state.steptime!)
            seq.append(act2)

            let act3 = SCNAction.run { _ in
                self.playAnimation(key: "stepR")
            }
            seq.append(act3)
            seq.append(act2)

            let act4 = SCNAction.run { _ in
                self.playAnimation(key: "idle")
            }
            seq.append(act4)

            let act5 = SCNAction.wait(duration: state.pausetime!)
            seq.append(act5)
        }

        let sequence = SCNAction.sequence(seq)
        scnView.scene?.rootNode.runAction(sequence, completionHandler:nil )