为什么animationDidStart:不起作用?

时间:2010-07-28 14:32:04

标签: iphone core-animation

我想在动画开始和停止时收到通知,所以我的代码是:

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

我确实实现了这两种方法,但animationDidStop:finished:已收到通知,animationDidStart:没有收到通知。

这是我的实施:

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

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

当我尝试直接拨打animationDidStart:animationDidStop:finished:时,我的应用程序崩溃并报告无法找到选择器。但是根据CAAnimation.h中的以下几行,如果我导入QuatzCore框架,NSObject的所有实例都应该响应这两个方法。我的理解是否正确?

/* 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

3 个答案:

答案 0 :(得分:2)

阅读精细手册,我看

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.

所以我认为你至少可以让选择器吃正确的论据。

您似乎正在实施不同的协议,请查看UIView文档。

答案 1 :(得分:1)

根据UIView文档,setAnimationWillStartSelector:消息需要一个带有+ (void)beginAnimations:(NSString *)animationID context:(void *)context签名的选择器。您提供的选择器具有错误的签名,因此不会被调用。 NSObject的CAAnimationDelegate类别甚至没有记录,因此您可能需要确切知道自己在做什么。但是你的问题是选择器签名错误。

答案 2 :(得分:0)

在你的UIView动画块中,设置选择器应如下所示:

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