views:

131

answers:

1

I've been trying to make a short animation that reverses halfway through with a static method that can be called by my view controllers. This works fine.

But I need it to perform a selector when the reverse takes place, essentially in the middle of the animation. The method setAnimationDidStopSelector only fires when the entire animation is completed, not in the middle.

So I split the animation into two blocks. However, when I do this, the first animation is instantly completed, then the second one happens. I suspect it's because the animation is on the same view. Perhaps there can't be two separate animation blocks for the same view?

I've set up the second block with a different animation name, and with the appropriate delay, and even tried setAnimationBeginsFromCurrentState (even though it's discouraged in OS 4), but none seem to solve the problem.

Here's some sample code to get an idea of one of the ways I've tried. 'selector' is the method I need called halfway through (once the alpha is transparent).

[UIView beginAnimations:name context:view];
[UIView setAnimationDuration:(duration/2)];
[UIView setAnimationDelegate:delegate];
[UIView setAnimationDidStopSelector:selector];
[view setAlpha:0.0];
[UIView commitAnimations];

name = [name stringByAppendingString:@"_reverse"];
[UIView beginAnimations:name context:view];
[UIView setAnimationDelay:(duration/2)];
[UIView setAnimationDuration:(duration/2)];
[UIView setAnimationDelegate:delegate];
[UIView setAnimationDidStopSelector:selector];
[view setAlpha:1.0];
[UIView commitAnimations];

Edit: I want to avoid animation 'blocks' as I'd like the code to be compatible with pre-OS4. I'd also like to keep the method static so I can use it as a library method.

+1  A: 

Have you tried putting the second part of the animation into a method, and use a NSTimer or performSelector: withObject: afterDelay:?

I don't know if these work or not, but UIView animations and drawing stuff tend to have weird interactions when a lot of stuff is done concurrently. For example, you set a couple animations to go off one after another in a single method. Spacing them out so that the first animation has no knowledge of the upcoming one might help. I do realize that you do set a delay on the second one, but UIViews are weird in that way.

Edit: Just tried it out, works like a charm.

David Liu
"Just tried 'it' out, works like a charm." Which way did you try? I moved the code to a second block, moved it to another static method, and even tried [delegate performSelector:selector]; (which doesn't work for some reason).
just_another_coder
Sweet - got it to work...I was using [delegate performSelector:@selector(selector)]; and didn't see the mistake. Thanks!
just_another_coder