Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Last active December 22, 2015 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcdilorenzo/6437406 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/6437406 to your computer and use it in GitHub Desktop.
CoreGraphics - clipping an inner circle (used in reference to a Stack Overflow question: http://stackoverflow.com/questions/18614376/calayer-clip-cgcontextaddarc-making-a-donut-slide-pie-chart
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) radius:CGRectGetHeight(self.bounds) startAngle:0 endAngle:2.0*M_PI clockwise:YES];
[path addArcWithCenter:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) radius:100 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
[path addClip];
self.layer.cornerRadius = 10.0;
CGFloat colors[12] = {
0.1, 0.18, 0.54, 1.0,
0.38, 0.44, 0.65, 1.0,
0.1, 0.18, 0.54, 1.0
};
CGFloat colorStops[3] = {0.0, 0.5, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,
colors,
colorStops,
3);
CGColorSpaceRelease(colorSpace);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(ctx, gradient, CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)), 100, CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)), CGRectGetHeight(self.bounds)/2, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment