Search code examples
iphonecore-animation

Why does animationDidStart: not work?


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

Solution

  • 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.