Last active
August 29, 2015 14:27
-
-
Save fcaldarelli/a84078ca7dc98885c539 to your computer and use it in GitHub Desktop.
UIView with radial background color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment