다음 코드는 빨간색에서 녹색으로 그라디언트가있는 반원을 그립니다. 이것은 내가 원하는 것이 아닙니다. 그라디언트로 그려진 너비 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);
}
링크 된 문서도 일례하지 반경 제외 OP가 원하던 (도 8-2)을 나타낸다. 프로세스를 설명하는 훌륭한 출발점이되어야합니다. – Doc