2009-04-24 170 views
7

爲了儘可能簡單地說出我的問題,有沒有辦法創建一個核心動畫序列來重複一遍又一遍,直到停止?核心動畫...循環動畫?

具體來說,我正在做一個自定義類,我想要一個-start和-stop方法,它會導致它脈動。編寫脈衝的動畫代碼不是問題,而是如何使其重複?

在此先感謝您的任何答案!

回答

15

the documentation,你做它用一個非常大的repeatCount創建動畫(從文檔中摘錄的代碼,我掛):

// create the animation that will handle the pulsing. 
CABasicAnimation* pulseAnimation = [CABasicAnimation animation]; 

// over a one second duration, and run an infinite 
// number of times 
pulseAnimation.duration = 1.0; 
pulseAnimation.repeatCount = HUGE_VALF; 

// we want it to fade on, and fade off, so it needs to 
// automatically autoreverse.. this causes the intensity 
// input to go from 0 to 1 to 0 
pulseAnimation.autoreverses = YES; 

編輯:在OP問及如何停止動畫。從文檔中的next paragraph

您通過 發送addAnimation:forKey:消息 到目標層,傳遞 動畫和標識符作爲參數 開始的明確的動畫。一旦添加到目標層 層,顯式動畫將運行 ,直到動畫完成,或者從圖層中刪除 。用於將動畫添加到 層的 標識符也用於通過 調用removeAnimationForKey:來停止它。您可以通過 發送圖層 removeAllAnimations消息來停止某個圖層的所有動畫。

+3

與您粘貼的代碼示例中的註釋相反,1e100不是無限的 - 僅爲10 ** 100。在math.h中定義的INFINITY常量會更好。 – 2009-04-24 03:20:28