2016-02-12 41 views
0

現在我有這個代碼,我希望能夠隨着弧的角度變化逐漸改變顏色。如何在Objective-C和Swift中使用UIColor逐漸改變顏色?

我想逐漸從綠色變爲黃色,然後是橙色,然後是紅色。但逐漸地,不只是一次。

有關如何做到這一點的任何想法?

UIColor *greenColor = [UIColor greenColor]; 
    greenColor = [UIColor colorWithRed:.0 green:1 blue:.0 alpha:0.5]; 

    UIColor *grayColor = [UIColor grayColor]; 
    grayColor = [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:0.5]; 

    UIColor *redColor = [UIColor redColor]; 
    redColor = [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5]; 

    UIColor *yellowColor = [UIColor yellowColor]; 
    yellowColor = [UIColor colorWithRed:1.0 green:1.0 blue:.0 alpha:0.5]; 

    UIColor *orangeColor = [UIColor orangeColor]; 
    orangeColor = [UIColor colorWithRed:1.0 green:0.647 blue:.0 alpha:0.5]; 

    int direction = 0; 
    if(index == 3){ 
     direction = 1; 
     CGFloat currentEndAngle = atan2f(d_y, d_x); 
     CGFloat actualEndAngle = -0.49234065359; //was M_PI 

     if(fabs(currentEndAngle*180/M_PI) <= 91){ 
      // Draw the optimal arc 
      //[self drawArc:ourPoint.startPosition andEndAngle:actualEndAngle andRadius:radius andColor:grayColor andDirection:direction withContext:context]; 

      if(currentEndAngle > 0 || currentEndAngle > actualEndAngle){ 
       // Draw the current green arc 
       if(currentEndAngle - actualEndAngle < 0.6){ 
        [self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:orangeColor andDirection:direction withContext:context]; 
       } 
       else{ 
        [self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:greenColor andDirection:direction withContext:context]; 
       } 

      } 
      else{ 
       // Draw the current red arc 
       [self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:redColor andDirection:direction withContext:context]; 
       missAngle += fabs(currentEndAngle-actualEndAngle); 
      } 
     } 
+0

請刪除您的swift標籤。這不是快速代碼... – Anokrize

回答

0

正如Ckouta說,在他的回答,你想要的是一個梯度。我認爲他的代碼不會完全按照你想要做的(用顏色漸變繪製一條弧線),但是這是朝正確方向邁出的一步。

獲取使用漸變繪製的弧線會很棘手。您可能需要創建自定義的Core Graphics代碼來自己渲染它,使用CALayers,圖層蒙版和CAGradientLayers的某種組合,或者可以使用Core Image濾鏡將徑向漸變應用於弧形圖像。