我在弄清楚如何使动画工作方面遇到困难。我有一个名为BallView的UIView。我可以画一个大的红色圆圈。我希望该圆圈从红色渐变为绿色。我在视图的初始化程序中设置了CAShapeLayerCABasicAnimation,但动画不起作用。这是我的初始值设定项:

- (void)initHelper:(UIColor*)c {
  CAShapeLayer* sl = (CAShapeLayer*) self.layer;
  sl.fillColor = [UIColor redColor].CGColor;
  sl.path = [[UIBezierPath bezierPathWithOvalInRect:self.bounds] CGPath];

  CABasicAnimation *colorAnim = [CABasicAnimation animationWithKeyPath:@"fillColor"];
  colorAnim.duration = 5.0;
  colorAnim.fromValue = [UIColor redColor];
  colorAnim.toValue = [UIColor greenColor];
  colorAnim.repeatCount = 10;
  colorAnim.autoreverses = YES;
  [sl addAnimation:colorAnim forKey:@"fillColor"];
}

最佳答案

您的from和to值不正确,您应该设置

 colorAnim.fromValue = (__bridge id)[UIColor redColor].CGColor;
 colorAnim.toValue = (__bridge id)[UIColor greenColor].CGColor;

10-05 20:05