Drawing a circular gradient

I would like to draw a circular gradient, as in the image below. I can easily control the radial gradient, but I'm not sure how to make a circular. I thought to draw a gradient on a line and then convert it to a circle. Is it possible?

enter image description here

This is how I draw a radial gradient:

CGFloat a = MIN(self.frame.size.width, self.frame.size.height) - _lineWidth; //Get current context CGContextRef mainContext = UIGraphicsGetCurrentContext(); CGRect newRect = rect; newRect.origin.x = ((self.frame.size.width - a)/2.0f); newRect.origin.y = ((self.frame.size.height - a)/2.0f); newRect.size.width = a; newRect.size.height = a; // Fill circle const CGFloat *_components = CGColorGetComponents(_fillColor.CGColor); CGFloat red = _components[0]; CGFloat green = _components[1]; CGFloat blue = _components[2]; CGFloat alpha = _components[3]; CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); CGFloat redBallColors[] = { red,green,blue,0.5, red,green,blue,alpha, }; CGFloat glossLocations[] = {0.0, 1.0}; CGGradientRef ballGradient = CGGradientCreateWithColorComponents(baseSpace, redBallColors, glossLocations, 2); CGPoint startPoint = self.center; CGPoint endPoint = self.center; CGContextDrawRadialGradient(mainContext, ballGradient, startPoint, 0, endPoint, a/2, 0); 

Thanks a lot!

+4
source share

All Articles