FabrizioCaldarelli
8/12/2015 - 3:38 PM

UIView with radial background color

UIView with radial background color

#import "RadialGradientBackgroundView.h"

#define COLOR_VERDE_1 [UIColor colorWithRed:((float)0x00/0xFF) green:((float)0x9A/0xFF) blue:((float)0xA1/0xFF) alpha:1]
#define COLOR_VERDE_2 [UIColor colorWithRed:((float)0x28/0xFF) green:((float)0xB2/0xFF) blue:((float)0xA2/0xFF) alpha:1]

@implementation RadialGradientBackgroundView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)drawRect:(CGRect)rect
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    NSArray *gradientColors = [NSArray arrayWithObjects:(id) COLOR_VERDE_2.CGColor, COLOR_VERDE_1.CGColor, nil];
    
    CGFloat gradientLocations[] = {0, 0.5, 1};
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) gradientColors, gradientLocations);
    
    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
    
    CGContextDrawRadialGradient(context, gradient, startPoint, 0, startPoint, MAX(rect.size.width, rect.size.height), kCGGradientDrawsBeforeStartLocation);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    

}

@end