Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2012 08:22
Show Gist options
  • Save anonymous/4266065 to your computer and use it in GitHub Desktop.
Save anonymous/4266065 to your computer and use it in GitHub Desktop.
Draw a radial gradient for view
//
// GradientView.m
// test
//
// Created by Sheng on 12-12-12.
// Copyright (c) 2012年 Sheng Jiang. All rights reserved.
//
#import "GradientView.h"
@implementation GradientView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)[UIColor whiteColor].CGColor,
(id)[UIColor blackColor].CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef)gradientColors,
gradientLocations);
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGFloat radius = MAX(CGRectGetHeight(rect), CGRectGetWidth(rect));
CGContextDrawRadialGradient(context, gradient,
center, 0,
center, radius,
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment