1

我想動畫CAShapeLayer的strokeColor,但在CABasicAnimation中,我有兩個值(from和to)。只有兩種顏色支持,而動畫是火嗎?例如,在開始我strokeColor = [UIColor blueColor].CGColor; 然後如何通過自定義漸變更改strokeColor

CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; 
colorAnimation.duration   = 3.0; // "animate over 3 seconds or so.." 
colorAnimation.repeatCount   = 1.0; // Animate only once.. 
colorAnimation.removedOnCompletion = NO; // Remain stroked after the animation.. 
colorAnimation.fillMode = kCAFillModeForwards; 
colorAnimation.toValue = (id)[UIColor redColor].CGColor; 
colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

在中途我有一個暗紫色的顏色,但我需要的,例如,黃色。

是否可以向CABasicAnimation添加自定義漸變?

回答

2

我不認爲你可以做到這一點CABasicAnimation,但你可以使用一個CAKeyframeAnimation爲動畫設置中間值:

CAKeyframeAnimation *colorAnimation = [CAKeyframeAnimation animationWithKeyPath:@"strokeColor"]; 
colorAnimation.values    = @[(id)[[UIColor blueColor] CGColor], 
             (id)[[UIColor yellowColor] CGColor], 
             (id)[[UIColor redColor] CGColor]]; 
colorAnimation.duration    = 3.0; // "animate over 3 seconds or so.." 
colorAnimation.repeatCount   = 1.0; // Animate only once.. 
colorAnimation.removedOnCompletion = NO; // Remain stroked after the animation.. 
colorAnimation.fillMode    = kCAFillModeForwards; 
colorAnimation.timingFunction  = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

如果你想要一個「跨越譜」之類的感覺,你可以這樣做:

colorAnimation.values = @[(id)[[UIColor blueColor] CGColor], 
          (id)[[UIColor greenColor] CGColor], 
          (id)[[UIColor yellowColor] CGColor], 
          (id)[[UIColor orangeColor] CGColor], 
          (id)[[UIColor redColor] CGColor]]; 

或者,如果你想要更多的一個簡單的藍色的紅色,但避免了真正的暗紫色,你可以這樣做:

colorAnimation.values = @[(id)[[UIColor blueColor] CGColor], 
          (id)[[UIColor colorWithRed:0.9 green:0.0 blue:0.9 alpha:1.0] CGColor], 
          (id)[[UIColor redColor] CGColor]]; 

很多選擇。

+1

非常非常感謝您的幫助和快速回答! =) – 2013-05-03 20:36:29

+0

@RostK。顯然,這會帶來很多選擇。用另外兩個例子更新了我的答案。 – Rob 2013-05-03 20:52:15

相關問題