The iPhone is waiting for the animation to complete

In an iPhone application, I am trying to capture animation ends using setAnimationDidStopSelector. I am trying to pause code execution until the animation ends. I've tried this; set the global variable BOOL, set it to TRUE before animating and after waiting for the animation using a while loop. In setAnimationDidStopSelector, set the BOOL variable to FALSE and hope the loop breaks. But unfortunately it didn't work, the code didn't even end up in setAnimationDidStopSelector (I'm checking this with some trace output). EDIT: If this BOOL variable handling is not added, the code runs in the handler method.

The code in which the 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 handler code:

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

      

What do you suggest?

+2


a source to share


4 answers


The animation will not start until this cycle is complete. This cycle will not complete until the animation starts.

while(![self AnimationEnded]);

      



Whatever you do after the animation, you need to go to the animationDidStop method.

-1


a source


In iOS 4, you can set a completion block instead of using delegate and animation handlers. This is an easier way to take action when your animation is over. I recommend using it if you don't support pre-iOS 4 devices.

Your example will change 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: animation: completion: on the iOS developer site for details .

+3


a source


You have to call setAnimationDelegate:

to assign the object you want to call when the animation stops. Assuming the method that sets your flag to FALSE is in the same class as the one in which you create the animation, this will be self

. See the UIView class reference for details .

+1


a source


Try the following:

__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

      

+1


a source







All Articles