【问题标题】:Animating CAShapeLayer's strokeColor with CABasicAnimation使用 CABasicAnimation 动画 CAShapeLayer 的 strokeColor
【发布时间】:2012-08-25 14:01:26
【问题描述】:

我使用this previous 线程的答案在CAShapeLayer 上为闪烁的笔划设置动画一直不成功,经过多次搜索后,我找不到其他使用CABasicAnimation 为笔划设置动画的示例。

我想要做的是让我的CAShapeLayer 脉冲在两种颜色之间。将CABasicAnimation 用于不透明度效果很好,但[CABasicAnimation animationWithKeyPath:@"strokeColor"] 让我无法理解,我会很感激有关如何成功实施的任何建议。

    CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
    strokeAnim.fromValue         = (id) [UIColor blackColor].CGColor;
    strokeAnim.toValue           = (id) [UIColor purpleColor].CGColor;
    strokeAnim.duration          = 1.0;
    strokeAnim.repeatCount       = 0.0;
    strokeAnim.autoreverses      = NO;
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"];

//    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
//    opacityAnimation.fromValue         = [NSNumber numberWithFloat:0.0];
//    opacityAnimation.toValue           = [NSNumber numberWithFloat:1.0];
//    opacityAnimation.duration          = 1.0;
//    opacityAnimation.repeatCount       = 0.0;
//    opacityAnimation.autoreverses      = NO;
//    [shapeLayer addAnimation:opacityAnimation forKey:@"animateOpacity"];

取消注释不透明度动画会导致预期的不透明度褪色。笔画动画不产生任何效果。隐式 strokeColor 更改按预期进行动画处理,但我希望确认 strokeColor 可以使用 CABasicAnimation 显式动画化。

更新:具体问题是 shapeLayer.path 为 NULL。更正它解决了问题。

【问题讨论】:

  • 逃避你...怎么办?你试过什么?它怎么不工作?会发生什么?

标签: objective-c ios core-animation cashapelayer


【解决方案1】:

下面的代码非常适合我。您的 shapeLayer 笔触路径的 lineWidth 是多少?会不会是这个问题?

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBezierPath * circle = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds];

    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = circle.CGPath;
    shapeLayer.strokeColor =[UIColor blueColor].CGColor;
    [shapeLayer setLineWidth:15.0];

    [self.view.layer addSublayer:shapeLayer];

    CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
    strokeAnim.fromValue         = (id) [UIColor redColor].CGColor;
    strokeAnim.toValue           = (id) [UIColor greenColor].CGColor;
    strokeAnim.duration          = 3.0;
    strokeAnim.repeatCount       = 0;
    strokeAnim.autoreverses      = YES;
    [shapeLayer addAnimation:strokeAnim forKey:@"animateStrokeColor"];

}

让我知道它是否适合你...

【讨论】:

  • 谢谢,这证实了 strokeColor 可以使用 CABasicAnimation 进行动画处理。我的问题一定出在其他地方(我怀疑路径的先前渲染或drawLayer中的后续渲染:inContext)。当我发现确切原因时,我会更新以供将来参考。
  • 用你的发现更新线程!我很想知道是什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多