[英]Core Animation - animation doesn't work
我正在嘗試使用此代碼為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
的實現...使用純色的作品)有關。 我仍在尋找這個問題的答案。
核心動畫通過使用插值進行工作-計算您指定的關鍵值之間的中間值。 如果是關鍵幀動畫,則會在values數組中的數值(n)之間進行插值。 如果是基本動畫,則會在兩個值之間進行插值-您的開始值和結束值。 CA可以使兩種純色之間的計算很好,因為它們僅由RGBA浮點值表示,但是它將如何在圖像之間插值? 它必須進行某種我不相信它能夠進行的變形。 如果需要變形效果,最好在視圖中的背景圖像之間進行過渡。 默認效果是交叉淡入/溶解效果。 那可能並不完全是您想要的,但可能會使您更加接近。
最好的祝福。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.