Wait until animation finishes before moving on Swift

别等时光非礼了梦想. 提交于 2019-12-12 03:58:39

问题


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!


回答1:


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

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

options are now option sets which are arrays. nil has become []

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

    self.view.alpha = 1.0

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


来源:https://stackoverflow.com/questions/32386296/wait-until-animation-finishes-before-moving-on-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!