Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created November 16, 2011 22:17
Show Gist options
  • Save codeswimmer/1371655 to your computer and use it in GitHub Desktop.
Save codeswimmer/1371655 to your computer and use it in GitHub Desktop.
iOS: Draw gradient fill
-(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