Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在设置动画时更改CALayer的颜色_Ios_Objective C_Calayer - Fatal编程技术网

Ios 如何在设置动画时更改CALayer的颜色

Ios 如何在设置动画时更改CALayer的颜色,ios,objective-c,calayer,Ios,Objective C,Calayer,我有一个圆弧,它绕着一个圆圈转,并且改变颜色三次。我在SO上使用了一个示例来开始: 这个例子是可行的,但是颜色是红色,然后是绿色,然后是蓝色。我希望它们是绿色,橙色,然后是红色。我试图弄清楚他是如何在代码中改变颜色的,这让我很困惑: for (NSUInteger i = 0; i < 3; i++) { CAShapeLayer* strokePart = [[CAShapeLayer alloc] init]; strok

我有一个圆弧,它绕着一个圆圈转,并且改变颜色三次。我在SO上使用了一个示例来开始:

这个例子是可行的,但是颜色是红色,然后是绿色,然后是蓝色。我希望它们是绿色,橙色,然后是红色。我试图弄清楚他是如何在代码中改变颜色的,这让我很困惑:

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];
我的目标是把这些颜色变成绿橙红,而不是红绿蓝

谢谢

编辑:

这是部分的值:

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

色调颜色系统以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];

部分的值是多少?它正好等于1/3,所以const double partition=1.0/((double)3);它相当于1/3或0.333333,对吗?是的,我的错,我刚刚得到了那份好工作!但是,如果您想要更多的代码改进,比如更少的条件和更灵活的方式,请查看我的答案。:)是的,这更干净。谢谢
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];