2010-04-08 41 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; 
}