简体   繁体   English

动画时如何更改CALayer的颜色

[英]How to change color of CALayer while animating

I have an arc that goes around in a circle and changes colors three times. 我有一个弧形,它绕一圈旋转,并且变色了三次。 I used an example on SO to get me started: Change CALayer color while animating 我在SO上使用了一个示例来开始学习:设置动画时更改CALayer颜色

This example works, however the colors are red, then green then blue. 此示例有效,但是颜色是红色,然后是绿色,然后是蓝色。 I would like them to be Green, Orange, then Red. 我希望它们是绿色,橙色,然后是红色。 I'm trying to figure out how in the code he changed colors, and it's quite confusing to me: 我试图弄清楚他如何在代码中更改颜色,这让我很困惑:

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

So I believe this line is doing the color change: 所以我相信这条线正在改变颜色:

strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];

My goal is to manipulate those colors to Green Orange Red instead of Red Green and Blue. 我的目标是将这些颜色操纵为绿色橙色红色,而不是红色绿色和蓝色。

Thanks 谢谢

EDIT: 编辑:

This is the value of portion: 这是part的值:

const double portion = 1.0 / ((double)3);

The HUE-Color system defines the color in a an angle of 360 degrees, where 0 is red, 108 (360*0,3) is a green and is a blue. HUE-Color系统以360度角定义颜色,其中0是红色,108(360 * 0,3)是绿色,是蓝色。 This is how the colors get generated: 这是生成颜色的方式:

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

If you want to change to colors, you could shift the portion by some other value: 如果要更改为颜色,则可以将该部分移动其他值:

strokePart.strokeColor = [[UIColor colorWithHue: 0.3 + portion saturation:1 brightness:1 alpha:1] CGColor];

Or you could define yourself an array, with your colors you want to go through: 或者,您可以定义自己的数组,并使用以下颜色:

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

Ah I just figured it out. 啊,我只是想通了。 Here is the answer below: 以下是答案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM