指を滑らせ、角度にあわせてスタンプを押していくペイントツール

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

ポイント

・touchesMovedで色々計算

・指を滑らせた方向にあわせてスタンプの角度を調整する

・指を滑らせた距離でスタンプを押す間隔を調整する

サンプルコード

#import “ViewController.h”

@interface Paw : UIView

@end

@implementation Paw

– (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

-(void)drawRect:(CGRect)rect

{

    // 肉球を書く

    [[UIColor colorWithRed:1.0 green:0.5 blue:0.5 alpha:1.0] setFill];

    

    float base = self.bounds.size.width*0.1;

    CGRect one = CGRectMake(base*0, base*2.8, 2.0*base, 2.4*base);

    CGRect two = CGRectMake(base*2.5, base*2.0, 2.0*base, 2.4*base);

    CGRect three = CGRectMake(base*5.0, base*2.0, 2.0*base, 2.4*base);

    CGRect four = CGRectMake(base*7.5, base*2.8, 2.0*base, 2.4*base);

    

    CGRect big = CGRectMake(base*2, base*5.5, 6.0*base, 4.0*base);

    

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextFillEllipseInRect(ctx, one);

    CGContextFillEllipseInRect(ctx, two);

    CGContextFillEllipseInRect(ctx, three);

    CGContextFillEllipseInRect(ctx, four);

    CGContextFillEllipseInRect(ctx, big);

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor blackColor];

    

    // 背景をパネルに

    for (int i=0; i<5; i++) {

        for (int j=0; j<5; j++) {

            float dx = self.view.bounds.size.width * 0.2;

            float dy = self.view.bounds.size.height * 0.2;

            float x = dx * i;

            float y = dy * j;

            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, dx, dy)];

            v.backgroundColor = (i+j)%2 ? [UIColor blackColor] : [UIColor darkGrayColor];

            [self.view addSubview:v];

        }

    }

    

    // タイトル

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(30, 400, 300, 50)];

    title.font = [UIFont fontWithName:@”Chalkduster” size:30];

    title.text = @”pawprint”;

    title.textColor = [UIColor whiteColor];

    title.backgroundColor = [UIColor clearColor];

    title.transform = CGAffineTransformMakeRotation(M_PI * 0.1);

    [self.view addSubview:title];

}

// p1 p2からarctangentで角度を算出

static CGPoint before;

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

{

    UITouch *t = [touches anyObject];

    before = [t locationInView:self.view];

}

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

{

    UITouch *t = [touches anyObject];

    CGPoint p = [t locationInView:self.view];

    

    float dif = hypot(p.xbefore.x, p.ybefore.y);

    if (dif > 30.0) {

        float angle = atan2(p.xbefore.x, p.ybefore.y) – M_PI * 1.0;

        

// 角度を確認するためのログ

// NSLog(@”x:%f, y:%f, angle:%f”, p.x – before.x, p.y – before.y, (angle/M_PI)*180);

        

        Paw *v = [[Paw alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

        v.center = p;

        v.transform = CGAffineTransformMakeRotation(-angle);

        [self.view addSubview:v];

        

        before = p;

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end