user192936
user192936

Reputation:

iPhone Wait For Animation Ending

In an iPhone application, I try to catch animation endings using setAnimationDidStopSelector. I try to suspend code execution until animation ends. I have tried this; set a global BOOL variable, set it to TRUE before commiting animation and after commiting animations waited using a while loop. In the setAnimationDidStopSelector, set the BOOL variable to FALSE and hope while loop to break. But unluckily this did not work, the code did not even fall into setAnimationDidStopSelector (I check that with some trace outputs). EDIT: If that BOOL variable handling is not added, code runs into the handler method.

The code where animation takes place is below:

self.AnimationEnded=FALSE;
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
// do sth.
[UIView commitAnimations];
while(![self AnimationEnded]);

Also this is the code of handler:

- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    printf("abc\n"); fflush(stdout);
    self.AnimationEnded=true;
}

What do you suggest?

Upvotes: 2

Views: 5348

Answers (4)

roberto.buratti
roberto.buratti

Reputation: 2497

Try this:

__block BOOL done = NO;
[UIView animateWithDuration:0.3 animations:^{
    // do something
} completion:^(BOOL finished) {
    done = YES;
}];
// wait for animation to finish
while (done == NO)
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
// animation is finished, ok to proceed

Upvotes: 1

Rob Bajorek
Rob Bajorek

Reputation: 6580

In iOS 4, you can set a completion block instead of using an animation delegate and handlers. This is a simpler way of taking action when your animation has ended. I recommend using it if you aren't supporting pre-iOS 4 devices.

Your example changes to:

self.animationEnded = NO;
[UIView animateWithDuration:2
        animations:^{ /* Do something here */ }
        completion:^(BOOL finished){
            printf("abc\n");
            fflush(stdout);
            self.animationEnded = YES;
        }];

See +UIView animateWithDuration:animations:completion: at the iOS developer site for more.

Upvotes: 3

drawnonward
drawnonward

Reputation: 53689

The animation will not start until this loop finishes. This loop will not finish until the animation starts.

while(![self AnimationEnded]);

Whatever you want to do after the animation needs to go in the animationDidStop method.

Upvotes: -1

Jason Jenkins
Jason Jenkins

Reputation: 5382

You have to call setAnimationDelegate:to designate the object that you want the selector called upon when the animation stops. Assuming that the method that sets your flag to FALSE is in the same class as the one where you are creating the animation this will be self. For details see the UIView class reference.

Upvotes: 1

Related Questions