2016-02-12 39 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滤镜将径向渐变应用于弧形图像。