2010-05-12 3 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

Я думаю, что вы хотите, радиальный градиент. См. Этот документ Apple.

https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

+0

Связанный документ даже показывает пример (рис 8-2), который является то, что хотел ОП, за исключением того, не как радиальной. Должна стать отличной отправной точкой, которая объясняет процесс. – Doc

-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); 
Смежные вопросы