How to fill a path with gradient in drawRect:?

Posted by Derrick on Stack Overflow See other posts from Stack Overflow or by Derrick
Published on 2010-06-06T18:28:48Z Indexed on 2010/06/06 18:32 UTC
Read the original article Hit count: 431

filling a path with a solid color is easy enough:

CGPoint aPoint;
for (id pointValue in points)
{
    aPoint = [pointValue CGPointValue];
    CGContextAddLineToPoint(context, aPoint.x, aPoint.y);
}
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);

I'd like to draw a gradient instead of solid red, but I am having trouble. I've tried the code listed in the Question/Answer: http://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone

which is:

CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setFrame:rect];
[gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor],
                (id)[[UIColor whiteColor] CGColor], nil]];
[[self layer] setMasksToBounds:YES];

[[self layer] insertSublayer:gradient atIndex:0];

However, this paints the entire view that this is in with the gradient, covering up my original path.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-graphics