How to wait until an animation is finished in Swift?

18,445

Solution 1

If you want to use the completion block at the end of the animation, you should use constructs with the delay and options arguments as well.

UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { 
// put here the code you would like to animate
    self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)

}, completion: {(finished:Bool) in
// the code you put here will be compiled once the animation finishes
    QueryInformations() 
})

Solution 2

Use the completion block on that function you're calling:

UIView.animateWithDuration(0.3, animations: {
        self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)
}, completion: {(finished:Bool) in
  QueryInformations()
})
Share:
18,445
Fabian Boulegue
Author by

Fabian Boulegue

Nerd Geek Videogamer Mac Developer Web Developer iOS Developer Tattoo

Updated on June 05, 2022

Comments

  • Fabian Boulegue
    Fabian Boulegue almost 2 years

    I try to let a button "shake" before the next question of my quiz is going to load.

    The animation looks like

    var timer = NSTimer()
        UIView.animateWithDuration(0.1, animations: {
            self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)
        })
        UIView.animateWithDuration(0.2, animations: {
            self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x - 4, self.IMGVIEWcat.center.y)
        })
        UIView.animateWithDuration(0.3, animations: {
                self.IMGVIEWcat.center = CGPointMake(self.IMGVIEWcat.center.x + 2, self.IMGVIEWcat.center.y)
        })
    

    After this the following function is called

        func QueryInformations(){
            println("QueryInformationsStart")
            self.ActivityIndicator.hidden = false
            ObjectIDsArrayCount = ObjectIDsArray.count
            var RandomQuestion = Int(arc4random_uniform(ObjectIDsArray.count + 0))
            var QuestionID:String = ObjectIDsArray[RandomQuestion]
            var query : PFQuery = PFQuery(className: "AddonQuiz")
            query.getObjectInBackgroundWithId(QuestionID){
                (ObjectHolder : PFObject!, error : NSError!) -> Void in
    ...
    

    Now the animation starts but instant the next question is loaded, is there some easy wait for a beginner to implement a "waiting" until the animation is done?

    I tried to set

    var complet == true 
    

    In the animation

    And in the query function

    if complet = true {....
    

    But this did not work for me, also I found some information about completion handler but didn't get it work in my code.

  • Fabian Boulegue
    Fabian Boulegue about 9 years
    This currently gives me a error, "missing argument for delay"
  • Fabian Boulegue
    Fabian Boulegue about 9 years
    getting a error on "}, completion: {" 'Bool' is not a Subtype of '()'
  • Cesare
    Cesare about 9 years
    Glad you got it working! it's really important you understand the options: nil block. Instead of nil you could use UIViewAnimationOptions.CurveLinear or UIViewAnimationOptions.CurveEaseOut. These settings modify how the animation behaves. If you use the last one the animation will begin quickly and the it will slow down as the animation completes.
  • Fabian Boulegue
    Fabian Boulegue about 9 years
    What you mean by UIViewAnimationOptions.CurveLinear or UIViewAnimationOptions.CurveEaseOut
  • Fabian Boulegue
    Fabian Boulegue about 9 years
  • Ian MacDonald
    Ian MacDonald about 9 years