Created
November 16, 2011 22:17
-
-
Save codeswimmer/1371655 to your computer and use it in GitHub Desktop.
iOS: Draw gradient fill
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
-(void)drawRect:(CGRect)rect | |
{ | |
CGContextRef context = UIGraphicsGetCurrnetContext(); | |
CGGradientRef gradient = [self gradient]; | |
CGPoint startPoint = CGPointMake(CGRectGetMidX(self.bounds), 0.0); | |
CGPoint endPoint = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.bounds)); | |
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint); | |
} | |
-(CGGradientRef)gradient | |
{ | |
if (nil == _gradient) | |
{ | |
CGFloat colors[6] = { | |
138.0f / 255.0f, 1.0f, | |
162.0f / 255.0f, 1.0f, | |
206.0f / 255.0f, 1.0f}; | |
CGFloat locations[3] = { 0.05f, 0.45f, 0.95f }; | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); | |
_gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, 3); | |
CGColorSpaceRelease(colorSpace); | |
} | |
return _gradient; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment