2010-05-12 13 views
5

Der folgende Code zeichnet einen Halbkreis mit einem Farbverlauf von Rot nach Grün. Das ist nicht was ich wollte. Ich erwartete einen Bogen der Breite 5 Pixel, der mit dem Gradienten gemalt wird.Wie kann ich einen gefüllten Bogen zeichnen, d. H. Regenbogen?

Jede Hilfe, die zeigt, wo ich falsch gelaufen bin, wird sehr geschätzt.

Charles

-(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); 

}

Antwort

-3

Der resultierende Gradient, zu mir, sieht aus wie die Kulisse für eine Kraftstoffanzeige, so dass eine radiale Gradienten nicht die richtige Wirkung geben kann. Ich habe nicht viel Erfahrung mit Quarz, aber meine Idee ist es, eine Ellipse mit der Hintergrundfarbe zu füllen.

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); 
Verwandte Themen