LoginSignup
10
8

More than 5 years have passed since last update.

UIViewのdrawRect方法で役立つコード

Last updated at Posted at 2015-02-25

UIViewのdrawRect方法で役立つコード

まずはそのUIViewの本体にあるdrawRect:の通常パターン。

- (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);
}

助けてくれたtakabosoftさんにありがとうございました。

10
8
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8