Drawing Circular Gradient

吃可爱长大的小学妹 提交于 2019-12-21 02:43:13

问题


I would like to draw a circular gradient like in the example picture below. I can manage a radial gradient easily but I am not sure how to do a circular one. I was thinking of drawing a gradient on a line and then transforming it to a circle. Would that be possible?

This is how I draw the 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!

来源:https://stackoverflow.com/questions/23242638/drawing-circular-gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!