使用 Core 动画为 StrokeColor 制作动画
我正在尝试对 CAShapeLayar 的描边颜色属性进行动画处理。我浏览了文档,据说它是一个可动画的属性。该代码适用于对 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"];
提前致谢, 或者。
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
The code I've used:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可行,正如我刚刚测试的那样:
请注意,我从动画中删除了
fromValue
属性行,因为您需要与在动画之前设置描边颜色相同的值(它是多余的)。另外,我将 borderColor 的 CGColorRef 转换为 id,这正是
toValue
属性想要的。This should work, as I just tested it:
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.接受的答案代码的一个重要陷阱是 addAnimation 方法的 forKey: 参数必须是要阻止触发其隐式动画的属性的名称。由于您正在使用 CABasicAnimation 创建显式动画,因此您不希望隐式动画和新创建的显式动画相互干扰。请参阅 WWDC 2010 视频,了解第 424 场核心动画 - 核心动画实践部分1 在第 39 分钟标记进行确认。
An important pitfall of the accepted answer code is that the forKey: parameter of the addAnimation method needs to be the name of the property whose implicit animation you want to prevent from firing. Since you are creating an explicit animation with CABasicAnimation, you don't want both the implicit animation and the newly-created explicit animation from interfering with one another. Refer to the WWDC 2010 video for Session 424 Core Animation - Core Animation in Practice, Part 1 at the 39 minute mark for confirmation.