2010-05-12 5 views
5

다음 코드는 빨간색에서 녹색으로 그라디언트가있는 반원을 그립니다. 이것은 내가 원하는 것이 아닙니다. 그라디언트로 그려진 너비 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); 

}

답변

-3

얻어진 기울기 내게되므로 방사형 그라데이션 적절한 효과를 제공하지 않을 수 연료 게이지에 대한 배경 같다. 저는 석영에 대한 많은 경험이 없지만 제 생각은 배경색을 사용하여 타원을 채우는 것입니다.

enter image description here

radius -= 5.0; 
CGRect rainbowArch = CGRectMake(x - radius, y - radius, 2 * radius, 2 * radius); 
const CGFloat * bgComponents = CGColorGetComponents(self.backgroundColor.CGColor); 
CGContextSetRGBFillColor(context, bgComponents[0], bgComponents[1], bgComponents[2], 1.0); 
CGContextFillEllipseInRect(context, rainbowArch); 

... 
CGContextRestoreGState(context); 

CGGradientRelease(gradientFill); 
CGColorSpaceRelease(colorSpace); 
관련 문제