使用CABasicAnimation动画CAShapeLayer的strokeColor

Mic*_*old 3 core-animation objective-c cashapelayer ios

我一直不成功,在一个动画闪烁的行程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"];
Run Code Online (Sandbox Code Playgroud)

取消注释不透明度动画会导致预期的不透明度淡化.笔画动画不起作用.隐式strokeColor按预期更改动画,但我想记录确认strokeColor可以使用CABasicAnimation显式动画.

更新:具体问题是shapeLayer.path为NULL.纠正修复问题.

小智 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"];

}
Run Code Online (Sandbox Code Playgroud)

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