2011-04-09 180 views
1

我試圖使用此代碼動畫一個CALayer的背景色:核心動畫 - 動畫不起作用

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 
animation.duration = 0.3; 
animation.fromValue = (id)[backgroundLayer backgroundColor]; 
animation.toValue = (id)[[UIColor redColor] CGColor]; 
[backgroundLayer addAnimation:animation forKey:@"animateBackgroundColor"]; 

出於某種原因,這並不做任何事情。只需用:

[backgroundLayer setBackgroundColor:[[UIColor redColor] CGColor]]; 

作品(雖然沒有動畫),並使用:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
animation.duration=0.3; 
animation.fromValue = [NSNumber numberWithFloat:1.0]; 
animation.toValue = [NSNumber numberWithFloat:0.0]; 
[backgroundLayer addAnimation:animation forKey:@"animateOpacity"]; 

作品,太(動畫)。

爲什麼我不能爲背景顏色設置動畫效果?

編輯:的問題與我的backgroundColor率與圖案圖像創建(使用UIImage的實現......使用純色作品)的事實。我仍然在尋找這個問題的答案。

回答

3

核心動畫通過使用插值工作 - 計算您指定的鍵值之間的中間值。如果它是關鍵幀動畫,它會在值數組中的數值(n)之間進行插值。如果它是基本動畫,它會在兩個值之間進行插值 - 開始和結束值。 CA可以在兩種純色之間進行計算,因爲這些只是由RGBA浮點值表示的,但它如何在圖像之間插值?它必須做某種形式的變形,我不相信它是有能力的。如果您需要變形效果,您可能更適合在視圖中的背景圖像之間切換。默認效果是淡入淡出/淡化效果。這可能不是你想要的,但它可能會讓你非常接近。

此致敬禮。