簡體   English   中英

動畫時如何更改CALayer的顏色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM