I have a UIView
which I want to have a radial gradient, and I'm wondering how to go about doing this?
Creating a layer with gradient colors is quite simple. For the most basic gradient, it only requires the colors you want to use, and then you can optionally adjust color location. Once you've created your gradient, add it simply to your view layer by calling the addSublayer function.
I worked off Karlis and Alexander's answers. I aimed to simplify as much as possible. Such as removing color space and locations (nil
) so the gradient uses the defaults.
Create file and add this code: import UIKit
@IBDesignable
class RadialGradientView: UIView {
@IBInspectable var InsideColor: UIColor = UIColor.clear
@IBInspectable var OutsideColor: UIColor = UIColor.clear
override func draw(_ rect: CGRect) {
let colors = [InsideColor.cgColor, OutsideColor.cgColor] as CFArray
let endRadius = min(frame.width, frame.height) / 2
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
On Storyboard, set the UIView to the above RadialGradientView
in Identity Inspector:
Set the Inside Color and Outside Color for your gradient in Attributes Inspector and see the change on your Storyboard:
(Note: I made the UIView on the Storyboard big enough to so it fills the entire
First subclass a UIView:
@implementation UIRadialView
- (void)drawRect:(CGRect)rect
{
// Setup view
CGFloat colorComponents[] = {0.0, 0.0, 0.0, 1.0, // First color: R, G, B, ALPHA (currently opaque black)
0.0, 0.0, 0.0, 0.0}; // Second color: R, G, B, ALPHA (currently transparent black)
CGFloat locations[] = {0, 1}; // {0, 1) -> from center to outer edges, {1, 0} -> from outer edges to center
CGFloat radius = MIN((self.bounds.size.height / 2), (self.bounds.size.width / 2));
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
// Prepare a context and create a color space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create gradient object from our color space, color components and locations
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorComponents, locations, 2);
// Draw a gradient
CGContextDrawRadialGradient(context, gradient, center, 0.0, center, radius, 0);
CGContextRestoreGState(context);
// Release objects
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
@end
And then add it to your view:
UIRadialView *radialView = [[UIRadialView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
radialView.backgroundColor = [UIColor redColor];
[self.view addSubview:radialView];
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With