如何绘制渐变填充弧,即彩虹?

CBr*_*uer 6 iphone objective-c ios

以下代码绘制了一个半圆,其中包含从红色到绿色的渐变.这不是我想要的.我期望用渐变绘制宽度为5像素的弧.

任何有助于展示我出错的地方都将不胜感激.

查尔斯

-(void) DrawRainbow {
// Create an arc path
float x = 150.0;
float y = 220.0;
float radius = 75.0;
float startAngle = M_PI;
float endAngle   = 2*M_PI;
bool clockWise = false;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, nil, x, y, radius, startAngle, endAngle, clockWise);

// Setup the gradient
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
    1.0, 0.0, 0.0, 1.0,   // Start color is red
    0.0, 1.0, 0.0, 1.0 }; // End color is green
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradientFill = 
    CGGradientCreateWithColorComponents (colorSpace, components,
                                         locations, num_locations);
// setup gradient points
CGRect pathRect = CGPathGetBoundingBox(path);
CGPoint myStartPoint, myEndPoint;
myStartPoint.x = CGRectGetMinX(pathRect);
myStartPoint.y = CGRectGetMinY(pathRect);
myEndPoint.x   = CGRectGetMaxX(pathRect);
myEndPoint.y   = CGRectGetMinY(pathRect);

// draw the gradient
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextAddPath(context, path);
CGContextSaveGState(context);
CGContextClip(context);
CGContextDrawLinearGradient (context, gradientFill,
                             myStartPoint, myEndPoint, 0);
CGContextRestoreGState(context);

CGGradientRelease(gradientFill);
CGColorSpaceRelease(colorSpace);
Run Code Online (Sandbox Code Playgroud)

}