如何在动画时更改 CALayer 的颜色
How to change color of CALayer while animating
我有一个圆弧,它绕了一圈并且改变了三次颜色。我在 SO 上使用了一个示例来帮助我入门:Change CALayer color while animating
这个例子有效,但是颜色是红色,然后是绿色,然后是蓝色。我希望它们是绿色、橙色,然后是红色。我想弄清楚他是如何在代码中改变颜色的,这让我很困惑:
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];
我的目标是将这些颜色处理为绿橙红,而不是红绿蓝。
谢谢
编辑:
这是部分的价值:
const double portion = 1.0 / ((double)3);
啊,我刚刚明白了。以下是答案:
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;
}
HUE-Color系统定义了360度角的颜色,其中0为红色,108(360*0,3)为绿色,108为蓝色。这就是颜色的生成方式:
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];
我有一个圆弧,它绕了一圈并且改变了三次颜色。我在 SO 上使用了一个示例来帮助我入门:Change CALayer color while animating
这个例子有效,但是颜色是红色,然后是绿色,然后是蓝色。我希望它们是绿色、橙色,然后是红色。我想弄清楚他是如何在代码中改变颜色的,这让我很困惑:
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];
我的目标是将这些颜色处理为绿橙红,而不是红绿蓝。
谢谢
编辑:
这是部分的价值:
const double portion = 1.0 / ((double)3);
啊,我刚刚明白了。以下是答案:
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;
}
HUE-Color系统定义了360度角的颜色,其中0为红色,108(360*0,3)为绿色,108为蓝色。这就是颜色的生成方式:
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];