views:

99

answers:

3

I'm tring to get notified when animation starts and stops, so my code is:

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
[UIView setAnimationWillStartSelector:@selector(animationDidStart:)];

I do implement these 2 methods, but animationDidStop:finished: got notified, and animationDidStart: did not.

Here's my implementation:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
}

- (void)animationDidStart:(CAAnimation *)anim
{
}

When I tried to call animationDidStart: or animationDidStop:finished: directly, my app crashed and reported that the selector could not be found. But according to following lines in CAAnimation.h, if I import QuatzCore framework, all the instances of NSObject should response to these 2 methods. Is my understanding correct?

/* Delegate methods for CAAnimation. */

@interface NSObject (CAAnimationDelegate)

/* Called when the animation begins its active duration. */

- (void)animationDidStart:(CAAnimation *)anim;

/* Called when the animation either completes its active duration or
 * is removed from the object it is attached to (i.e. the layer). 'flag'
 * is true if the animation reached the end of its active duration
 * without being removed. */

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

@end
+2  A: 

Reading The Fine Manual, I see

The selector should have the same arguments as the beginAnimations:context: method,
an optional application-supplied identifier and context. Both of these arguments can
be nil.

So I presume you could at least make the selector eat the right arguments.

It seems you are implementing a different protocol, have a look at the UIView docs.

mvds
Yes, this should be the reason why the delegate was not called. I'll try it late and will add a report here.But I still have a little bit confusion that CAAnimationDelegate category to NSObject should be valid, when I called them directly.
MQ Gu
?? don't exactly understand you, but it doesn't work like that. You set the delegate, and the selector. Then the selector is called with 2 arguments, the identifier and the context (that may just be `nil`). That's it. No `NSObject`, no `CAAnimationDelegate`, no category. `CAAnimation` is just another way of animating things. Don't mix it up with the `UIView` way of animating.
mvds
+1  A: 

According to the UIView documentation the setAnimationWillStartSelector: message expects a selector with a signature like the + (void)beginAnimations:(NSString *)animationID context:(void *)context. The selector you provide has a wrong signature and will therefor not be called. The CAAnimationDelegate category to NSObject is not even documented, so you probably need to know exactly what you are doing. However your problem is the wrong selector signature.

thatsdisgusting
I'm a little confused. I think CAAnimationDelegate category to NSObject is documented in:http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html#//apple_ref/occ/instm/NSObject/animationDidStart:
MQ Gu
a word on terminology: `CAAnimation` is not a "category", but "inherits" from `NSObject`, which says nothing more than that it is, in fact, an object. `CAAnimationDelegate` is a protocol, which is a set of rules telling which methods you should / may provide to establish some interaction.
mvds
@MQ Gu: You are right, i took not enough time for research, sorry! But it doesn't change anything with your problem. CAAnimation is an abstract class, used (implemented) by UIView to perform he basic animations. You need to use the UIView defined selector to make your code work.
thatsdisgusting
@mvds: I never wrote that CAAnimation is a category. But "CAAnimationDelegate" is indeed a category extending NSObject. There is no @protocol definition of "CAAnimationDelegate" in the sense of Objective-C protocols. That way you may provide default implementations for every Object, avoid gcc protocol warning terror and keep backwards compatibility to pre 2.0.
thatsdisgusting
@disgusting: Sorry about that, I responded to MQ who started about categories. But you're right, thanks for the info!
mvds
@disgusting and @mvds: thank you very much!
MQ Gu
A: 

In your UIView animation block, setting the selectors should look like this:

[UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
cookeecut