首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用核心动画制作StrokeColor动画

使用核心动画制作StrokeColor动画
EN

Stack Overflow用户
提问于 2011-08-16 23:41:43
回答 2查看 2.9K关注 0票数 3

我正在尝试动画CAShapeLayar的strokeColor属性。我看了一下文档,上面写着它是一个动画属性。代码可以为postion.y设置动画,但不能为strokeColor设置动画。我很乐意得到任何形式的帮助或建议

我使用的代码如下:

代码语言:javascript
复制
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"];

提前谢谢,或者。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-17 03:06:07

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

代码语言:javascript
复制
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.

请注意,我从动画中删除了fromValue属性线,因为您希望在动画之前设置与strokeColor相同的值(它是多余的)。另外,我将strokeColor的CGColorRef转换为id,这正是toValue属性想要的。

票数 6
EN

Stack Overflow用户

发布于 2012-08-31 11:03:35

公认答案代码的一个重要缺陷是,addAnimation方法的forKey:参数需要是您希望阻止触发其隐式动画的属性的名称。由于使用CABasicAnimation创建显式动画,因此不希望隐式动画和新创建的显式动画相互干扰。请参考第424节核心动画-核心动画实践的WWDC 2010 video,第1部分39分钟标记处进行确认。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7080952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档