IOS - How to draw a gradient along a path

This is the effect I want to achieve:

enter image description here

This is the effect I have now:

enter image description here

Basically, I need to draw a gradient along the arc path, not linearly along the arc. I've tried many ways, some of them give the same result, but some of them give a result that is further from the expectation.

This is the code I have right now:

    CGFloat lineSize = 12;
    CGFloat radius = self.frame.size.width/2 - lineSize;

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //Define gradient
    CGFloat colors [] =
    {
        0.9451f, 0.5804f, 0.2431f, 1.0f,
        0.9922f, 0.9490f, 0.9020f, 1.0f,
        1.0f, 1.0f, 1.0f, 1.0f,
        0.8706f, 0.9020f, 0.9412f, 1.0f,
        0.2941f, 0.6863f, 0.9176f, 1.0f
    };

    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 5);
    CGColorSpaceRelease(baseSpace), baseSpace = NULL;

    //Define arc angle and points
    CGFloat startAngle = 3.0f/4.0f * M_PI;
    CGFloat endAngle = 1.0f/4.0f * M_PI;

    CGFloat a = radius * cos(45);
    CGFloat o = a * tan(45);

    CGFloat y = CGRectGetMidY(rect) + o;

    CGPoint startPoint = CGPointMake(0, y);
    CGPoint endPoint = CGPointMake(self.frame.size.width, y);

    CGMutablePathRef arc = CGPathCreateMutable();
    CGPathAddArc(arc, NULL, CGRectGetMidX(rect), CGRectGetMidY(rect), radius,
                 startAngle, endAngle, NO);

    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, 10, kCGLineCapButt, kCGLineJoinMiter, 10);

    CGContextAddPath(ctx, strokedArc);
    CGContextClip(ctx);

    CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient);

      

I also tried to change this block of code:

CGMutablePathRef arc = CGPathCreateMutable();
    CGPathAddArc(arc, NULL, CGRectGetMidX(rect), CGRectGetMidY(rect), radius,
                 startAngle, endAngle, NO);

    CGPathRef strokedArc = CGPathCreateCopyByStrokingPath(arc, NULL, 10, kCGLineCapButt, kCGLineJoinMiter, 10);

    CGContextAddPath(ctx, strokedArc);
    CGContextClip(ctx);

    CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient);

      

to this block code:

CGContextAddArc(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect), radius, startAngle, endAngle, NO);

CGContextSetLineWidth(ctx, lineSize);
CGContextSetLineCap(ctx, kCGLineCapRound);

CGContextReplacePathWithStrokedPath(ctx);
CGContextClip(ctx);

CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient), gradient = NULL;

      

But still gives the same result as the one I have now.

+3


source to share


1 answer


Try changing the gradient values.

Change

 //Define gradient
    CGFloat colors [] =
    {
        0.9451f, 0.5804f, 0.2431f, 1.0f,
        0.9922f, 0.9490f, 0.9020f, 1.0f,
        1.0f, 1.0f, 1.0f, 1.0f,
        0.8706f, 0.9020f, 0.9412f, 1.0f,
        0.2941f, 0.6863f, 0.9176f, 1.0f
    };

      



Something symmetrical. how

    CGFloat colors [] =
    {
        1.0f, 0.9176f, 0.6863f, 0.2941f,
        1.0f, 0.9412f, 0.9020f, 0.8706f,
        0.7432f, 1.0f, 1.0f, 0.7432f,
        0.8706f, 0.9020f, 0.9412f, 1.0f,
        0.2941f, 0.6863f, 0.9176f, 1.0f
    };

      

0


source







All Articles