Created
December 12, 2012 08:22
-
-
Save anonymous/4266065 to your computer and use it in GitHub Desktop.
Draw a radial gradient for view
This file contains hidden or 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
// | |
// 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