迅速,从一个阵列连续播放多个动画?

时间:2018-09-18 23:15:09

标签: swift animation

对不起,我是新手。我不能让每个动画连续播放而不是一次播放。我尝试使用sleep(),但是那似乎不允许动画播放。这就是我找到要播放的动画的方式。

for number in sequence {
    switch number {
    case 1:
        print("blue")
        animateB()
    case 2:
        print("green")
        animateG()
    case 3:
        print("magenta")
        animateM()
    case 4:
        print("orange")
        animateO()
    case 5:
        print("yellow")
        animateY()
    case 6:
        print("red")
        animateR()
    case 7:
        print("purple")
        animateP()
    case 8:
        print("cyan")
        animateC()
    default:
        print("error")
    }
}

这是我用来制作动画的功能之一。我意识到这可能效率也很低,但不确定如何使功能更好。

private func animateB(){
    let animation = CABasicAnimation(keyPath: "transform.scale")
    animation.toValue = 1.3
    animation.duration = 0.5
    animation.autoreverses = true
    self.pulsatingB.add(animation, forKey: "pulsing")
}

任何帮助将非常感谢。 :)

2 个答案:

答案 0 :(得分:1)

您可以使用CATransaction来链接CAAnimation

class ViewController: UIViewController {

    // The animations, to be applied in order
    var animationQueue = [() -> Void]()

    @IBAction func animate(_ sender: Any) {

        animationQueue.removeAll()

        // Build the animation queue
        for number in sequence {
            switch number {
            case 1:
                print("blue")
                animationQueue.append(animateB)
            case 2:
                print("green")
                animationQueue.append(animateG)
            // ....
            default:
                break
            }
        }

        // Start the animation
        applyNextAnimation()
    }

    func applyNextAnimation() {
        guard !animationQueue.isEmpty else { return }
        let animation = animationQueue.removeFirst()

        // When an animation completes, call this function again to apply the next animation
        CATransaction.begin()
        CATransaction.setCompletionBlock({ self.applyNextAnimation() })
        animation()
        CATransaction.commit()
    }

}

答案 1 :(得分:1)

对于一系列动画,基于块的关键帧动画通常也可以完成这项工作,例如:

UIView.animateKeyframes(withDuration: 4.0, delay: 0, options: .repeat, animations: {
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.25, animations: {
        self.subview.transform = .init(scaleX: 0.5, y: 0.5)
    })

    UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
        self.subview.transform = .init(scaleX: 1.3, y: 1.3)
    })

    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.25, animations: {
        self.subview.transform = .init(scaleX: 0.75, y: 0.75)
    })

    UIView.addKeyframe(withRelativeStartTime: 0.75, relativeDuration: 0.25, animations: {
        self.subview.transform = .identity
    })
}, completion: nil)

或者,如果您有一系列功能:

let animations = [animateA, animateB, animateC, animateD]

UIView.animateKeyframes(withDuration: 4.0, delay: 0, options: .repeat, animations: {
    for (index, animation) in animations.enumerated() {
        UIView.addKeyframe(withRelativeStartTime: Double(index) / Double(animations.count), relativeDuration: 1 / Double(animations.count), animations: {
            animation()
        })
    }
}, completion: nil)

在哪里

func animateA() {
    subview.transform = .init(scaleX: 0.5, y: 0.5)
}

func animateB() {
    subview.transform = .init(scaleX: 1.3, y: 1.3)
}

...