Last active
December 21, 2015 16:09
-
-
Save xdream86/6331767 to your computer and use it in GitHub Desktop.
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
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