Skip to content

Instantly share code, notes, and snippets.

@sinri
Last active August 29, 2015 14:16
Show Gist options
  • Save sinri/e13185ba8d110d30b0a4 to your computer and use it in GitHub Desktop.
Save sinri/e13185ba8d110d30b0a4 to your computer and use it in GitHub Desktop.
UIViewのdrawRect方法で役立つコード ref: http://qiita.com/sinri/items/5fef1e9722fd222f3696
- (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: 具体的な描くコード
}
/**
以下の方法は、
[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);
}
-(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);
}
-(void)drawLineFrom:(CGPoint)startPoint to:(CGPoint)endPoint inContext:(CGContextRef) context {
// 指定する二つの点の間に連接する線
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
}
-(void)drawFilledRect:(CGRect)rect inContent:(CGContextRef)context{
CGContextFillRect(context, rect);
}
-(void)drawStrokedRect:(CGRect)rect inContent:(CGContextRef)context{
CGContextStrokeRect(context, rect);
}
-(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