Created
May 1, 2015 20:29
-
-
Save bluwave/c9e7d3175977dd70ca6e to your computer and use it in GitHub Desktop.
Draw circle with a gradient color border, clear center
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
| // Draw circle | |
| CGContextSaveGState(context); | |
| CGFloat lineWidth = 2.0f; | |
| CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2); | |
| UIBezierPath *outerCirclePath = [UIBezierPath bezierPath]; | |
| [outerCirclePath addArcWithCenter:center radius:(rect.size.height / 2) - insetPad/2 startAngle:0 endAngle:2 * M_PI clockwise:YES]; | |
| UIBezierPath *innerCirclePath = [UIBezierPath bezierPath]; | |
| [innerCirclePath addArcWithCenter:center radius:((rect.size.height / 2) - lineWidth - (insetPad/2)) startAngle:0 endAngle:2 * M_PI clockwise:YES]; | |
| [outerCirclePath appendPath:innerCirclePath]; | |
| outerCirclePath.usesEvenOddFillRule = YES; | |
| [outerCirclePath addClip]; | |
| // Circle gradient | |
| CGContextAddPath(context, outerCirclePath.CGPath); | |
| CGContextClip(context); | |
| CGFloat locations[3] = {1.0, 0.70, 0.0}; | |
| UIColor *alphaColor = [self.borderColor colorWithAlphaComponent:0]; | |
| NSArray *colors = @[(id)alphaColor.CGColor, (id)alphaColor.CGColor, (id)self.borderColor.CGColor]; | |
| CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); | |
| CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, locations); | |
| CGContextDrawLinearGradient(context, gradient, CGPointMake(rect.size.width, 0), CGPointMake(-1, 0), kCGGradientDrawsAfterEndLocation); | |
| CGColorSpaceRelease(colorspace); | |
| CGGradientRelease(gradient); | |
| CGContextSetLineWidth(context, self.borderWidth); | |
| CGContextStrokePath(context); | |
| CGContextRestoreGState(context); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment