等待动画完成,然后继续进行Swift [英] Wait until animation finishes before moving on Swift

查看:81
本文介绍了等待动画完成,然后继续进行Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我如何等待动画完成后继续前进?我希望代码将发送脉冲两次,然后将发送脉冲一次,但据我所知,它直接转到已发送。

In the following code, how do I wait until the animation is done before moving on? I want the code to pulse "Sending" twice and "Sent" once, but it goes straight to "Sent", as far as I can tell"

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)

我如何等待动画的一部分完成,然后再继续进行下一部分?谢谢!

How can I wait for one part of the animation to finish before it moves on to the next part? Thanks!

推荐答案

这就是完成处理程序的用途。将下一个动画放入上一个动画的完成范围内。

That is what the completion handlers are for. Place the next animation inside the completion of the previous one.

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

Drop this in a playground. If you split it up in functions you can keep it readable. But this will be broken next Wednesday.

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()

当optio出现时,swift2不喜欢它ns为零:

It appears swift2 doesn't like it when the options are nil:

选项现在是数组的选项集。 nil已变成[]

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

    self.view.alpha = 1.0

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

这篇关于等待动画完成,然后继续进行Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆