1

我想为 CAShapeLayer 的 strokeColor 设置动画,但在 CABasicAnimation 中我有两个值(从和到)。动画火了是不是只有两种颜色支持?例如,在开始时我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 添加自定义渐变?

4

1 回答 1

2

我不认为你可以用 来做到这一点CABasicAnimation,但你可以使用 aCAKeyframeAnimation为你的动画设置中间值:

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]];

很多选择。

于 2013-05-03T20:29:25.943 回答