【问题标题】:CAShapeLayer not AnimatedCAShapeLayer 没有动画
【发布时间】:2014-10-27 00:55:39
【问题描述】:

我无法弄清楚如何让动画正常工作。我有一个名为 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"];
}

【问题讨论】:

  • 你在哪里调用了这个方法?是BallView的初始化方法吗?
  • 我重写了其他初始化方法。这些 init 方法调用 initHelper。

标签: ios core-animation


【解决方案1】:

你的 from 和 to 值不对,你应该设置

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

【讨论】:

    【解决方案2】:

    您不应该在 init 方法中放置动画,因为当时这个视图被添加到视图层次结构中。尝试将 initHelper 方法移出 init 方法并将其暴露在 BallView.h 中,这样您就可以在视图控制器中随时调用它,您可以在 viewDidAppear 方法中添加它。如果您不想这样做,您可以像这样简单地延迟 initHelper 的触发时间:

    [self performSelector:@selector(initHelper:) withObject:nil afterDelay:2.0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2017-01-20
      • 2014-03-08
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多