使用Core动画为StrokeColor设置动画

Or *_*Ron 3 core-animation core-graphics ios

我正在尝试为CAShapeLayar的strokeColor属性设置动画.我查看了文档,并声明它是一个可动画的属性.该代码用于动画postion.y但不是strokeColor.我很乐意得到任何帮助或建议

我用过的代码:

lyr.fillColor = [[UIColor clearColor] CGColor];
lyr.lineWidth = 3.8;
lyr.strokeColor = [[UIColor yellowColor] CGColor];
lyr.position = CGPointMake(160, 431);
lyr.anchorPoint = CGPointMake(.5f, .5f);
[self.view.layer addSublayer:lyr];

CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
basicAnimation.fromValue = [lyr valueForKey:@"strokeColor"];



basicAnimation.toValue =[NSValue valueWithPointer:[[UIColor clearColor] CGColor]];


basicAnimation.duration = 2;
basicAnimation.repeatCount = 10;
basicAnimation.autoreverses = YES;
[lyr addAnimation:basicAnimation forKey:@"strokeColor"];
Run Code Online (Sandbox Code Playgroud)

在此先感谢,或者.

Mig*_*elB 6

这应该工作,因为我刚测试它:

CABasicAnimation *strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeColor"]; // I changed the animation pointer name, but don't worry.
basicAnimation.toValue = (id) [UIColor clearColor].CGColor; // Just get rid of the fromValue line, and just cast the CGColorRef to an id.
basicAnimation.duration = 2;
basicAnimation.repeatCount = 10;
basicAnimation.autoreverses = YES;
[lyr addAnimation:strokeAnim forKey:@"flashStrokeColor"]; // I like to name this key differently, so as not create confusion.
Run Code Online (Sandbox Code Playgroud)

请注意,我fromValue从动画中删除了属性行,因为您需要与在动画之前设置strokeColor相同的值(冗余).此外,我将strokeColor的CGColorRef转换为id,这正是该toValue属性所需要的.