views:

194

answers:

3

I'm concerned that this is impossible, because +setAnimationDelegate: of UIView is a class method. But maybe I am wrong with that?

Background: The problem is, that I have many objects of the same class, and I want to implement a method which does some nice animations specially for that object. Those animations are a little complex and consist of a few phases. So I need to be notified when an animation stopped. Now, it may happen that 10 objects from that class start animating at same time.

+1  A: 

The only way to set different animation delegates for multiple objects is to have them be of separate subclasses of UIView. As you thought, since +setAnimationDelegate: is a class method, you can't set separate animation delegates for different instances of the same class.

Tim
That makes sense.
Thanks
+2  A: 

Each animation block has its own delegate. +[UIView setAnimationDelegate:] and +[UIView setAnimationDidStopSelector:] only do anything when called in between +[UIView beginAnimations:context:] and +[UIView commitAnimations], and only affect the animation set up by that block.

Kevin Ballard
+1  A: 

The +[UIView beginAnimations:context:] method allows you to pass along a specific context that is passed along to the completion method. You can use that context to disambugate between the various instances when the completion method is called.

Since the context is typed as a (void *) it can be pretty much anything you want it to be, i.e. pointer to an object instance, a unique ID, or a custom struct.

If your objects all implement a common protocol you can pass them along as the context and in the animationDidStop method, just invoke the method defined by the protocol. So even though you have one single class-wide animationDidStop method it can act as a fan-out method dispatcher.

Ramin
Cool. So for example I could set "self" as the context when calling beginAnimations? I'll try that right away.
Thanks