等到动画结束后再继续使用Swift

时间:2015-09-03 22:17:11

标签: swift core-animation

在下面的代码中,我如何等待动画完成后再继续?我希望代码能够发出脉冲"发送"两次"已发送"曾经,但它直接发送到#34;发送",据我所知"

UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 0.0
            }, completion: nil)
        countdownLabel.text = "Sending"
        UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 1.0
            }, completion: nil)
        UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 0.0
            }, completion: nil)
        countdownLabel.text = "Sending"
        UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 1.0
            }, completion: nil)
        UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 0.0
            }, completion: nil)
        countdownLabel.text = "Sent"
        cancelButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        UIView.animateWithDuration(1.0, delay: 0.25, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.countdownLabel.alpha = 1.0
            }, completion: nil)

如何在动画移动到下一部分之前等待动画的一部分完成?谢谢!

1 个答案:

答案 0 :(得分:3)

这就是完成处理程序的用途。将下一个动画置于上一个动画的完成内。

将它放在操场上。如果将其拆分为函数,则可以使其可读。但这将在下周三打破。

var view = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
view.backgroundColor = UIColor.blackColor()


func animationOne() {
    UIView.animateWithDuration(1.0,
        delay: 0.0,
        options: nil,
        animations: {view.alpha = 1},
        completion: {finished in animationTwo()})
}

func animationTwo() {
    UIView.animateWithDuration(1.0,
        delay: 0.0,
        options: nil,
        animations: {view.alpha = 0},
        completion: {finished in animationThree()})
}

func animationThree() {
    UIView.animateWithDuration(1.0,
        delay: 0.0,
        options: nil,
        animations: {view.alpha = 1},
        completion: {finished in print("done")})

}

animationOne()

当选项为nil时,似乎swift2不喜欢它:

选项现在是选项集,它们是数组。 nil已成为[]

UIView.animateWithDuration(2.0, delay: 0.0, options: [], animations: { () -> Void in

    self.view.alpha = 1.0

    }, completion: { (finished: Bool) -> Void in
     // next animation
})