简体   繁体   English

使用CABasicAnimation动画CAShapeLayer的strokeColor

[英]Animating CAShapeLayer's strokeColor with CABasicAnimation

I've been unsuccessful at animating a flashing stroke on a CAShapeLayer using the answer from this previous thread, and after many searches I can find no other examples of animating the stroke using CABasicAnimation . 我一直不成功,在一个动画闪烁的行程CAShapeLayer使用的答案从这个以前的线程,许多搜索后,我可以找到使用动画的行程没有其他例子CABasicAnimation

What I want to do is have the stroke of my CAShapeLayer pulse between two colors. 我想要做的是让我的CAShapeLayer脉冲在两种颜色之间。 Using CABasicAnimation for opacity works fine, but the [CABasicAnimation animationWithKeyPath:@"strokeColor"] eludes me, and I'd appreciate any advice on how to successfully implement. 使用CABasicAnimation进行不透明效果很好,但[CABasicAnimation animationWithKeyPath:@"strokeColor"]使我[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"];

Uncommenting the opacity animation results in an expected opacity fade. 取消注释不透明度动画会导致预期的不透明度淡化。 The stroke animation produces no effect. 笔画动画不起作用。 An implicit strokeColor change animates as expected, but I would like documented confirmation that strokeColor can be explicitly animated using CABasicAnimation. 隐式strokeColor按预期更改动画,但我想记录确认strokeColor可以使用CABasicAnimation显式动画。

Update: The specific problem was that shapeLayer.path was NULL. 更新:具体问题是shapeLayer.path为NULL。 Correcting that fixed the problem. 纠正修复问题。

The code below works great for me. 下面的代码对我很有用。 What is the lineWidth of your shapeLayer stroke path? shapeLayer笔划路径的lineWidth是多少? Could that be the issue? 这可能是问题吗?

- (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"];

}

Let me know if it works for you... 请让我知道这对你有没有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM