2017-02-02 3 views
2

원을 그리며 3 번 색상을 변경하는 원호가 있습니다. 그래서 나를 시작하기 위해 예제를 사용했습니다. Change CALayer color while animating움직이는 동안 CALayer의 색상을 변경하는 방법

이 예제는 작동하지만 색상은 녹색, 녹색, 파란색입니다. 나는 그 (것)들을 녹색, 주황색, 그 후에 빨간색으로하고 싶다. 나는 코드에서 그 색상을 변경하는 방법을 알아 내려고 노력하고있어, 그것은 나에게 매우 혼란 :

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

내 목표는 : 그래서

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); 
+0

'부분'값은 무엇입니까? – Kie

+0

단지 1/3이므로 const double 부분 = 1.0/((double) 3); –

+0

오히려 1/3 또는 0.333333과 비슷합니다. 맞습니까? – Kie

답변

2

색조 컬러 시스템이 0 적색 360도, 108 (* 0.3 360)의 각도에서의 색상을 정의하는 녹색 및 청색이다 여기서 아래 응답이다. 당신이 색상으로 변경하려면, 당신은 몇 가지 다른 값 부분을 이동할 수

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

예 제공되는이 청소기가 훨씬 큽니다. 감사! –

1

아, 난 그냥 그것을 알아 냈다.

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

잘 했어! 그러나 더 적은 조건과 더 유연한 내 대답을 확인하는 등 더 많은 코드 개선이 필요한 경우 :) <3 – Kie

관련 문제