Last active
August 29, 2015 14:16
-
-
Save sinri/e13185ba8d110d30b0a4 to your computer and use it in GitHub Desktop.
UIViewのdrawRect方法で役立つコード ref: http://qiita.com/sinri/items/5fef1e9722fd222f3696
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 { | |
// contextを取得 | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// 残っていた痕跡をクリア | |
CGContextClearRect(context, rect); | |
// 背景色が必要ならここで設定 | |
UIColor * _boardColor = [UIColor whiteColor]; | |
[self changeFillColorTo:(_boardColor) inContext:context]; | |
[self changeStrokeColorTo:(_boardColor) inContext:context]; | |
[self drawRect:rect inContent:context]; | |
// 直線の端のスタイル。ここは四角いが、円形になるも可能 | |
CGContextSetLineCap(context, kCGLineCapSquare); | |
// 直線の幅を設定 | |
CGContextSetLineWidth(context, 1.0); | |
// TODO: 具体的な描くコード | |
} |
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
/** | |
以下の方法は、 | |
[color setStroke]; | |
と同じです。 | |
*/ | |
-(void)changeStrokeColorTo:(UIColor*)color inContext:(CGContextRef)context{ | |
const CGFloat* components = CGColorGetComponents(color.CGColor); | |
CGFloat red=components[0]; | |
CGFloat green=components[1]; | |
CGFloat blue=components[2]; | |
CGFloat alpha=CGColorGetAlpha(color.CGColor); | |
CGContextSetRGBStrokeColor(context, red, green, blue, alpha); | |
} | |
/** | |
以下の方法は、 | |
[color setFill]; | |
と同じです。 | |
*/ | |
-(void)changeFillColorTo:(UIColor*)color inContext:(CGContextRef)context{ | |
const CGFloat* components = CGColorGetComponents(color.CGColor); | |
CGFloat red=components[0]; | |
CGFloat green=components[1]; | |
CGFloat blue=components[2]; | |
CGFloat alpha=CGColorGetAlpha(color.CGColor); | |
CGContextSetRGBFillColor(context, red, green, blue, alpha); | |
} |
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)drawPoint:(CGPoint)point inContext:(CGContextRef)context{ | |
CGFloat pointSize=1; | |
CGContextFillEllipseInRect(context, CGRectMake(point.x, point.y, pointSize, pointSize)); | |
} | |
-(void)drawKeyPoint:(CGPoint)point inContext:(CGContextRef)context{ | |
// キーポイントだから、十字の照準記号を描く | |
CGFloat targetCrossSize=5; | |
CGContextMoveToPoint(context, point.x-targetCrossSize, point.y); | |
CGContextAddLineToPoint(context, point.x+targetCrossSize, point.y); | |
CGContextStrokePath(context); | |
CGContextMoveToPoint(context, point.x, point.y-targetCrossSize); | |
CGContextAddLineToPoint(context, point.x, point.y+targetCrossSize); | |
CGContextStrokePath(context); | |
} |
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)drawLineFrom:(CGPoint)startPoint to:(CGPoint)endPoint inContext:(CGContextRef) context { | |
// 指定する二つの点の間に連接する線 | |
CGContextMoveToPoint(context, startPoint.x, startPoint.y); | |
CGContextAddLineToPoint(context, endPoint.x, endPoint.y); | |
CGContextStrokePath(context); | |
} |
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)drawFilledRect:(CGRect)rect inContent:(CGContextRef)context{ | |
CGContextFillRect(context, rect); | |
} | |
-(void)drawStrokedRect:(CGRect)rect inContent:(CGContextRef)context{ | |
CGContextStrokeRect(context, rect); | |
} |
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)drawFilledCircleWithin:(CGRect)rect inContext:(CGContextRef)context{ | |
CGContextFillEllipseInRect(context, rect); | |
} | |
-(void)drawStrokedCircleWithin:(CGRect)rect inContext:(CGContextRef)context{ | |
CGContextStrokeEllipseInRect(context, rect); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment