iPhoneお顔

小さい子向けに、点をなぞって描くiPhoneアプリを描いてみます。以前にも点をなぞって遊ぶアプリを書いたのですが、今回はそれの顔練習版です。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController () {

    AVAudioPlayer *driveSound;

    AVAudioPlayer *coinSound;

    UIBezierPath *path;

    CAShapeLayer *sl;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    [self initSound];

    [self layoutCoins];

}

– (void)layoutCoins

{

    CGRect rect[] = {CGRectMake(50, 50, 300, 300), CGRectMake(400, 50, 300, 300)};

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

        UIView *circle = [[UIView alloc] initWithFrame:rect[i]];

        circle.backgroundColor = [UIColor whiteColor];

        circle.layer.cornerRadius = 150;

        circle.layer.borderColor = [UIColor lightGrayColor].CGColor;

        circle.layer.borderWidth = 20;

        

        [self.view addSubview:circle];

        float r = circle.bounds.size.width * 0.44;

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

            float th = (M_PI / 10.0) * i;

            float x = circle.center.x + r * cos(th);

            float y = circle.center.y + r * sin(th);

            [self createCoinAtPoint:CGPointMake(x, y)];

        }

    }

    

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(160, 600, 400, 20)];

    line.backgroundColor = [UIColor blackColor];

    [self.view addSubview:line];

    

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

        

        [self createCoinAtPoint:CGPointMake(i*40 + 180, 600)];

    }

}

– (void)createCoinAtPoint:(CGPoint)p

{

    int dx = arc4random() % 105;

    int dy = arc4random() % 105;    

    UIView *coin = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    coin.center = CGPointMake(p.x + dx, p.y + dy);

    coin.backgroundColor = [self color:arc4random() % 5];

    coin.layer.cornerRadius = 10.0;

    [self.view addSubview:coin];

    coin.tag = 1;

}

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

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    path = [UIBezierPath bezierPath];

    [path moveToPoint:p];

    

    sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [self color:arc4random() % 5].CGColor;

    sl.lineWidth = 10;

    [self.view.layer addSublayer:sl];

}

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

{

    if (!driveSound.isPlaying) {

        [driveSound play];

    }

    

    CGPoint p = [[touches anyObject] locationInView:self.view];

    for (UIView *v in self.view.subviews) {

        if (v.tag == 1 && CGRectContainsPoint(v.frame, p)) {

            coinSound.currentTime = 0;

            [coinSound play];

            

            [UIView animateWithDuration:0.5 animations:^{

                float angle = (M_PI / 10.0) * (arc4random() % 20);

                float x = 1000 * cos(angle);

                float y = 1000 * sin(angle);

                v.transform = CGAffineTransformMakeTranslation(x, y);

            } completion:^(BOOL finished) {

                [v removeFromSuperview];

            }];

        }

    }

    

    [path addLineToPoint:p];

    sl.path = path.CGPath;

}

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

{

    if ([self.view viewWithTag:1] == nil) {

        [self performSelector:@selector(restart) withObject:nil afterDelay:0.5];

    }

}

– (void)restart

{

    self.view.layer.sublayers = nil;

    [self layoutCoins];

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0x00FFD3);

        case 1:

            return UIColorHex(0x0007FF);

        case 2:

            return UIColorHex(0x880FFF);

        case 3:

            return UIColorHex(0xFFEB00);

        case 4:

            return UIColorHex(0x00FF45);

        default:

            break;

    }

    return nil;

}

#pragma mark – sound

– (void)initSound

{

    NSString *fpath = [[NSBundle mainBundle] pathForResource:@”engine” ofType:@”m4a”];

    driveSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fpath] error:nil];

    

    fpath = [[NSBundle mainBundle] pathForResource:@”coin” ofType:@”mp3″];

    coinSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fpath] error:nil];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end