Skip to content

Instantly share code, notes, and snippets.

@xdream86
Last active December 21, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdream86/6331767 to your computer and use it in GitHub Desktop.
Save xdream86/6331767 to your computer and use it in GitHub Desktop.
static inline double radians (double degrees) { return degrees * M_PI/180; }
@implementation MyLayerDelegate
- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext {
CGContextSetStrokeColorWithColor(theContext, [UIColor greenColor].CGColor);
CGMutablePathRef path = createArcPathFromBottomOfRect(CGRectMake(0, 0, 100, 100), 50.0f);
CGContextBeginPath(theContext);
CGContextAddPath(theContext, path);
CGContextSetLineWidth(theContext, 3);
CGContextStrokePath(theContext);
CFRelease(path);
}
CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight) {
CGRect arcRect = CGRectMake(rect.origin.x,
rect.origin.y + rect.size.height - arcHeight,
rect.size.width, arcHeight);
CGFloat arcRadius = (arcRect.size.height/2) +
(pow(arcRect.size.width, 2) / (8*arcRect.size.height));
CGPoint arcCenter = CGPointMake(arcRect.origin.x + arcRect.size.width/2,
arcRect.origin.y + arcRadius);
CGFloat angle = acos(arcRect.size.width / (2*arcRadius));
CGFloat startAngle = radians(180) + angle;
CGFloat endAngle = radians(360) - angle;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius,
startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment