iOS, Draw radial gradient that fills recrangle
Asked Answered
K

1

6

The code below draws a perfect elliptical radial gradient, but does not fill in the corners of it's view. How do I get it to draw beyond the edge of the ellipse? The documented option is kCGGradientDrawsAfterEndLocation, but I think it's not available in ios.

- (void)drawRect:(CGRect)rect
{
    CGFloat colors [] = {
        0.2, 0.2, 0.2, 1.0,
        0.0, 0.0, 0.0, 1.0
    };
    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
    CGColorSpaceRelease(baseSpace), baseSpace = NULL;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextAddEllipseInRect(context, rect);
    CGContextClip(context);
    CGContextDrawRadialGradient(context, gradient, self.center, 0, self.center, self.frame.size.width, kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(gradient), gradient = NULL;
    CGContextRestoreGState(context);
}

enter image description here

Kratz answered 5/1, 2013 at 8:17 Comment(0)
A
4

You've clipped the drawing to the ellipse. That stops the gradient being drawn outside the clipping area. Remove the line where you add the ellipse and clip the context.

Alfieri answered 5/1, 2013 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.