2010-07-28 83 views
1

我特林当动画的启动和停止,所以我的代码是得到通知:为什么animationDidStart:不起作用?

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

我实现这些2种方法,但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 

回答

1

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

+0

我有点困惑。我认为CAAnimationDelegate类别与NSObject记录在: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html#//apple_ref/occ/instm/NSObject/ animationDidStart: – 2010-07-28 16:51:27

+0

关于术语的一个词:'CAAnimation'不是一个“类别”,而是“NSObject”的“继承”,它只是说实际上是一个对象。 'CAAnimationDelegate'是一个协议,它是一组规则,告诉你应该/可能提供哪些方法来建立一些交互。 – mvds 2010-07-28 17:36:41

+0

@MQ顾:你是对的,我没有足够的时间进行研究,对不起!但它不会改变你的问题。 CAAnimation是一个抽象类,由UIView使用(实现)来执行他基本的动画。你需要使用UIView定义的选择器来使你的代码工作。 – thatsdisgusting 2010-07-28 22:00:04

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文档。

+0

是的,这应该是为什么委托是不叫的原因。我会尽快尝试,并在此处添加报告。 但我仍然有一点困惑,CAAnimationDelegate类别NSObject应该是有效的,当我直接调用它们。 – 2010-07-28 16:58:57

+0

??不完全了解你,但它不是那样工作。您设置委托和选择器。然后选择器被调用2个参数,标识符和上下文(可能只是'nil')。而已。没有'NSObject',没有'CAAnimationDelegate',没有类别。 'CAAnimation'只是另一种动画事物的方式。不要将它与'UIView'动画方式混合在一起。 – mvds 2010-07-28 17:34:49

0

在你的UIView动画块,设置选择应该是这样的:

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