3

我在使用上一个线程的答案为 a 上的闪烁笔划设置动画时一直不成功CAShapeLayer经过多次搜索,我找不到其他使用.CABasicAnimation

我想做的是让我的CAShapeLayer脉搏在两种颜色之间跳动。使用CABasicAnimationfor opacity 效果很好,但是让[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。纠正这个问题就解决了。

4

1 回答 1

7

下面的代码对我很有用。您的 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"];

}

请让我知道这对你有没有用...

于 2012-08-25T15:18:43.547 回答