views:

65

answers:

2

I have an implict animation as follows:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];

...

[UIView commitAnimations];

When this animation begins and ends, it triggers these delegate functions:

- (void)animationWillStart:(NSString *)animationID context:(void *)context;

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag;

I have multiple things that get animated in my viewController, and the delegate functions are getting crossed. How do I tag a particular animation so I can check which one it is in the delegate functions?

It's also odd to me that the parameter for one of these functions is a string while the other is a CAAnimation. Both of these methods get called, but am I using the wrong one or something?

+3  A: 

For implicit animations like that, you set your animationDidStopSelector:

[UIView setAnimationDidStopSelector:@selector(animationDidEnd:finished:context:)];

and that will call your animationDidEnd as follows:

- (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

This is pretty much the same as the animationWillStartSelecter which calls beginAnimations:context:

For both, you can use context to pass in a tag or other values that your selectors can use to distinguish the animation.

mahboudz
+1  A: 
[UIView beginAnimations:nil context:nil];

Am I crazy here or is there some reason you are passing in nil for animationID (first argument)? No need to mess with context, you can simply set the ID and then look at the ID passed into your didStop selector. That's what it's there for!

Kendall Helmstetter Gelner
Ah, thanks. I guess I didn't realize what the first argument was for. I did get this working by setting the animationDidEnd selector to a unique function, but it's good to know how to set the ID and use the delegate method properly. +1
Andrew Johnson