我想为CAShapeLayer的strokeColor设置动画,但在CABasicAnimation中我有两个值(from和to)。动画是火的时候只有两种颜色支持吗?例如,一开始我有strokeColor = [UIColor blueColor].CGColor;
然后
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添加自定义渐变?
答案 0 :(得分: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]];
很多选择。