How to wait until an animation is finished in Swift?
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()
})
Fabian Boulegue
Nerd Geek Videogamer Mac Developer Web Developer iOS Developer Tattoo
Updated on June 05, 2022Comments
-
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 about 9 yearsThis currently gives me a error, "missing argument for delay"
-
Fabian Boulegue about 9 yearsgetting a error on "}, completion: {" 'Bool' is not a Subtype of '()'
-
Cesare about 9 yearsGlad you got it working! it's really important you understand the
options: nil
block. Instead ofnil
you could useUIViewAnimationOptions.CurveLinear
orUIViewAnimationOptions.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 about 9 yearsWhat you mean by UIViewAnimationOptions.CurveLinear or UIViewAnimationOptions.CurveEaseOut
-
Fabian Boulegue about 9 yearsLet us continue this discussion in chat.
-
Ian MacDonald about 9 yearsIt shouldn't. Taken straight from developer.apple.com/library/ios/documentation/UIKit/Reference/…: