tags:

views:

171

answers:

2

Hi,

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?

+1  A: 

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.

Jason Jenkins
I didn't get; edited my question and added code...
paul simmons
A: 

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.

drawnonward
even if it's outside animation block?
paul simmons
The iPhone OS is geared towards run loops. Instead of sitting in your own tight loop waiting for something, register a callback or set a timer and let the run loop run.
drawnonward