UIViewのdrawRectで人形を書く方法のメモ

(Xcode の iOS6 Simulatorで試しています。)

ポイント

・drawRect

・CGContext

タッチで棒人間を画面にどんどん表示していくサンプルのコード

[Stickperson.h]

@interface StickPerson : UIView {

    CGPoint head;

    CGFloat headSize;

    CGPoint shoulder;

    CGPoint body;

    CGPoint lhand, rhand;

    CGPoint lfoot, rfoot;

}

// – (void)walk:(CGPoint)center;

@end

[Stickperson.m]

@implementation StickPerson

– (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    

    if (self) {

        // 基本設定

        

        // 背景を透過

        self.backgroundColor = [UIColor clearColor];

        

        // 基本の長さ(8分割)

        float base = frame.size.height / 8.0;

        

        // View内部の中心座標

        CGPoint midPoint;

        midPoint.x = self.bounds.origin.x + self.bounds.size.width / 2;

        midPoint.y = self.bounds.origin.y + self.bounds.size.height / 2;

        

        // 頭の座標

        float hx = midPoint.x;

        float hy = base;

        head = CGPointMake(hx, hy);

        headSize = base;

        

        

        // 肩の座標

        shoulder = CGPointMake(hx, hy + headSize);

        

        

        // 腰の座標

        float bx = head.x;

        float by = head.y + 3 * base;

        body = CGPointMake(bx, by);

        

        

        // 左手

        float lhx = shoulder.x + 2 * base * cos(M_PI / 4.0);

        float lhy = shoulder.y + 2 * base * sin(M_PI / 4.0);

        lhand = CGPointMake(lhx, lhy);

        

        

        // 右手

        float rhx = shoulder.x2 * base * cos(M_PI / 4.0);

        float rhy = shoulder.y + 2 * base * sin(M_PI / 4.0);

        rhand = CGPointMake(rhx, rhy);

        

        

        

        // 左足

        float lfx = body.x + 2 * base * cos(M_PI / 4.0);

        float lfy = body.y + 2 * base * sin(M_PI / 4.0);

        lfoot = CGPointMake(lfx, lfy);

        

        

        // 右足

        float rfx = body.x2 * base * cos(M_PI / 4.0);

        float rfy = body.y + 2 * base * sin(M_PI / 4.0);

        rfoot = CGPointMake(rfx, rfy);

    }

    return self;

}

-(void)drawRect:(CGRect)rect

{

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    // 塗りつぶしの色をセット

    [[UIColor grayColor] set];

    // 線の色をセット

    [[UIColor grayColor] setStroke];

    CGContextSetLineWidth(context, 5.0);

    

    

    //

    // 丸を塗りつぶし

    UIGraphicsPushContext(context);

    CGContextBeginPath(context);

    CGContextAddArc(context, head.x, head.y, headSize, 0, 2*M_PI, YES);

    CGContextFillPath(context);

    UIGraphicsPopContext();

    

    

    // 胴体

    // 直線を一本ひく

    UIGraphicsPushContext(context);

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, head.x, head.y);

    CGContextAddLineToPoint(context, body.x, body.y);

    CGContextStrokePath(context);

    UIGraphicsPopContext();

    

    

    // ,

    UIGraphicsPushContext(context);

    CGContextBeginPath(context);

    //

    CGContextMoveToPoint(context, shoulder.x, shoulder.y);

    CGContextAddLineToPoint(context, lhand.x, lhand.y);

    CGContextMoveToPoint(context, shoulder.x, shoulder.y);

    CGContextAddLineToPoint(context, rhand.x, rhand.y);

    //

    CGContextMoveToPoint(context, body.x, body.y);

    CGContextAddLineToPoint(context, lfoot.x, lfoot.y);

    CGContextMoveToPoint(context, body.x, body.y);

    CGContextAddLineToPoint(context, rfoot.x, rfoot.y);

    CGContextStrokePath(context);

    UIGraphicsPopContext();

    

    

}

@end

[ViewController.m]

#import “ViewController.h”

#import “StickPerson.h”

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    UITouch *touch = [touches anyObject];

    

    StickPerson *sp = [[StickPerson alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    sp.center = [touch locationInView:self.view];

    [self.view addSubview:sp];

}

@end