2010-04-08 71 views
15

我按照页面底部的示例调用animationDidStop函数。CABasicAnimation代表animationDidStop?

http://www.informit.com/articles/article.aspx?p=1168314&seqNum=2

作者说:

我有一个专门为动画的委托的对象,它是所有持有到目标对象的引用,接受animationDidStop:消息,然后释放自己。

这意味着你不应该做的:

[animation setDelegate:self]; 

我很新的应用程序编程有人可以说明我如何才能做到这一点?或者给我一个链接,在那里解释。

+0

你的意思是它应该[动画setDelegate:self]; ?文章说这个称呼。 – 2011-11-10 12:49:40

+1

__请注意''CAAnimation''''delegate'强,所以你可能需要将它设置为'nil'来避免保留周期!__ – 2016-08-10 12:36:01

回答

38

实现:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
您的委托对象上

。您也可以执行:

- (void)animationDidStart:(CAAnimation *)theAnimation 

在动画开始时接收呼叫。

欲了解更多信息,请参阅的代表部分: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

+8

这是正确的答案。 为什么有人对此投票,特别是没有评论? – thefaj 2010-07-29 20:54:15

+0

那么为什么文档说在UIView类参考部分为setAnimationDidStopSelector方法使用'animationDidStop:(NSString *)animationID完成:(NSNumber *)完成上下文:(void *)'? – CommaToast 2015-12-08 20:09:51

0

只设置

[UIView setAnimationDelegate:self]; 

不会叫任何的动画委托方法动画开始或结束时。

此问题可以通过以下解决方法之一来解决。

1)在您的实现部分添加

@implementation MyViewWithAnimations <UIApplicationDelegate> 


2)在动画开始提交块添加

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


3)执行苹果公司建议,使用块相反,基于动画的方法。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

+0

没有留言的投票可以帮助任何人。 – lindinax 2013-03-18 10:54:53

+0

尽管我并不是一个低调的人,但我相信这是因为您将“UIApplicationDelegate”添加到您的课程中,即使它与动画代理无关。 – 2015-08-09 21:55:44

4

有时候你的层的实际值设置为当需要在动画完成的toValue。当它是一个更复杂的动画,如动画CAGradientLayer的颜色时,这是必需的。

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    self.gradientLayer.colors = (NSArray *)((CABasicAnimation*)theAnimation).toValue; 
}