IOS, draw a radial gradient that fills the redraw

The code below draws a perfect elliptical radial gradient, but does not fill its corners. How to make him draw outside the ellipse? The documented parameter is kCGGradientDrawsAfterEndLocation, but I think it is 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

+6
source share
1 answer

You cut the drawing to an ellipse. This stops the gradient, which extends outside the clipping region. Delete the line where you add the ellipse and clamp the context.

+4
source

All Articles