CAShapeLayer没有动画
问题描述:
我无法搞清楚如何让动画工作。我有一个名为BallView的UIView。我可以画一个很大的红色圆圈。我希望这个圈子从红色变为绿色。我在我的视图的初始化器中设置了CAShapeLayer
和CABasicAnimation
,但动画不起作用。这里是我的初始化:CAShapeLayer没有动画
- (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"];
}
答
你从和值是不正确的,你应该设置
colorAnim.fromValue = (__bridge id)[UIColor redColor].CGColor;
colorAnim.toValue = (__bridge id)[UIColor greenColor].CGColor;
答
你不应该把动画中的init方法,因为当时这个观点是被添加到视图层次结构中。尝试将initHelper方法从init方法中移出并在BallView.h中公开,因此您可以在视图Controller中随时调用它,您可以将其添加到viewDidAppear方法中。如果你不想这样做,你可以简单地推迟initHelper这样的火时间:
[self performSelector:@selector(initHelper:) withObject:nil afterDelay:2.0];
你在哪里调用此方法?它是BallView的初始化方法吗? – gabbler 2014-10-27 01:39:04
我重写了其他init方法。那些init方法调用initHelper。 – csnate 2014-10-27 01:40:38