![](/img/trans.png)
[英]How to inherit animation properties while animating CALayer with implicit animation
[英]How to change color of CALayer while animating
我有一个弧形,它绕一圈旋转,并且变色了三次。 我在SO上使用了一个示例来开始学习:设置动画时更改CALayer颜色
此示例有效,但是颜色是红色,然后是绿色,然后是蓝色。 我希望它们是绿色,橙色,然后是红色。 我试图弄清楚他如何在代码中更改颜色,这让我很困惑:
for (NSUInteger i = 0; i < 3; i++)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
所以我相信这条线正在改变颜色:
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
我的目标是将这些颜色操纵为绿色橙色红色,而不是红色绿色和蓝色。
谢谢
编辑:
这是part的值:
const double portion = 1.0 / ((double)3);
HUE-Color系统以360度角定义颜色,其中0是红色,108(360 * 0,3)是绿色,是蓝色。 这是生成颜色的方式:
strokePart.strokeColor = [[UIColor colorWithHue: 0 saturation:1 brightness:1 alpha:1] CGColor]; // RED
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 saturation:1 brightness:1 alpha:1] CGColor]; // GREEN
strokePart.strokeColor = [[UIColor colorWithHue: 0.6 saturation:1 brightness:1 alpha:1] CGColor]; // BLUE
如果要更改为颜色,则可以将该部分移动其他值:
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 + portion saturation:1 brightness:1 alpha:1] CGColor];
或者,您可以定义自己的数组,并使用以下颜色:
NSArray* colors = @[ UIColor.green, UIColor.orange, UIColor.red ];
for (id color in colors)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [color CGColor];
啊,我只是想通了。 以下是答案:
if (i == 0) {
strokePart.strokeColor = [UIColor greenColor].CGColor;
}
else if (i == 1) {
strokePart.strokeColor = [UIColor orangeColor].CGColor;
}
else if (i == 2) {
strokePart.strokeColor = [UIColor redColor].CGColor;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.