简体   繁体   中英

Draw radial Gradient on UIView (circle)

unfortunately I can't get to a solution with drawing a radial gradient in my UIView.

Look...that's how I want to draw it (this was made in PS): 在此处输入图片说明

But I'm currently stuck on this: 在此处输入图片说明

That's my code:

CGGradientRef radialGradient;
CGColorSpaceRef rgbColorspace;

size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
const CGFloat *colorComponents = CGColorGetComponents(textColor.CGColor);
CGFloat components[8] = { colorComponents[0], colorComponents[1], colorComponents[2], 1.0,  // Start color
    1.0, 1.0, 1.0, 1.0 }; // End color

rgbColorspace = CGColorSpaceCreateDeviceRGB();
radialGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

CGRect currentBounds = self.bounds;
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));

CGContextDrawRadialGradient(myContext, radialGradient, midCenter, 8.0, midCenter, 1.0, kCGGradientDrawsAfterEndLocation);



CGGradientRelease(radialGradient);
CGColorSpaceRelease(rgbColorspace);

I hope someone can give me the right tip ;)

Enjoy the weekend, Chris

Basically, you need to use more than 2 locations. With 2 locations you get a constant gradient between the start and end locations. Your sample image shows the start colour for most locations up to the end and a much paler end colour. Looking at the sample image you will need at least 3 locations and to change the end colour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM