Created
August 6, 2012 05:16
-
-
Save miyakeryo/3270949 to your computer and use it in GitHub Desktop.
Draw Message Bubbles.
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
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextBubblePath(context, CGRectMake(60.5, 40.5, 170, 70)); | |
CGContextStrokePath(context); | |
} | |
//角度→ラジアン変換 | |
#if !defined(RADIANS) | |
#define RADIANS(D) (D * M_PI / 180) | |
#endif | |
//吹き出しを描く | |
void CGContextBubblePath(CGContextRef context, CGRect rect) | |
{ | |
CGFloat rad = 10; //角の半径 | |
CGFloat qx = 10; // くちばしの長さ | |
CGFloat qy = 20; // くちばしの高さ | |
CGFloat cqy = 4; // 上くちばしカーブの基準点の高さ | |
CGFloat lx = CGRectGetMinX(rect)+qx; //左 | |
CGFloat rx = CGRectGetMaxX(rect); //右 | |
CGFloat ty = CGRectGetMinY(rect); //上 | |
CGFloat by = CGRectGetMaxY(rect); //下 | |
CGContextBeginPath(context); | |
CGContextMoveToPoint(context, lx, ty+rad); //左上 | |
CGContextAddArc(context, lx+rad, ty+rad, rad, RADIANS(180), RADIANS(270), 0); //左上のカーブ | |
CGContextAddArc(context, rx-rad, ty+rad, rad, RADIANS(270), RADIANS(360), 0); //右上のカーブ | |
CGContextAddArc(context, rx-rad, by-rad, rad, RADIANS(0), RADIANS(90), 0); //右下のカーブ | |
CGContextAddArc(context, lx+rad, by-rad, rad, RADIANS(90), RADIANS(125), 0); //くちばしの付け根(下の凹み) | |
CGContextAddQuadCurveToPoint(context, lx, by, lx-qx, by); //くちばしの先端 | |
CGContextAddQuadCurveToPoint(context, lx, by-cqy, lx, by-qy); //くちばしの付け根(上) | |
CGContextClosePath(context); //左上の点まで閉じる | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment