使用Core动画为StrokeColor制作动画 [英] Using Core animation to animate StrokeColor

查看:94
本文介绍了使用Core动画为StrokeColor制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to animate CAShapeLayar's strokeColor property. I looked throught the documentation and it was stated that it is an animatable property. The code is working for animating postion.y but not the the strokeColor. I'll be happy to get any kind of help or advice

我使用过的代码:

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

提前致谢,
或。

Thanks in advance, Or.

推荐答案

这应该有效,因为我刚刚测试过:

This should work, as I just tested it:

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.

注意我从中删除​​了$ code>属性行动画,因为你想要在动画之前设置strokeColor的相同值(它是多余的)。
另外,我将strokeColor的CGColorRef转换为id,这正是 toValue 属性所需要的。

Notice I removed the fromValue property line from the animation, since you want the same value as you set the strokeColor before the animation (its redundant). Also, I casted the strokeColor's CGColorRef to an id, which is exactly what the toValue property wants.

这篇关于使用Core动画为StrokeColor制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆