iPhone数字が向かってくるアプリ

道路に書いた数字がこっちに向かってくるような子供の算数勉強用iPhoneアプリを作ってみようと思います。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *roadCanvas;

    UIView *player;

    NSTimer* timer;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self createRoadCanvas];

    

    [self addRoad:CGPointMake(160, 568.0/2.0)];

    [self addRoad:CGPointMake(160, –568.0/2.0)];

    [self addRoad:CGPointMake(160, –568.0 * 1.5)];

    [self addRoad:CGPointMake(160, –568.0 * 2.5)];

    

    [self createPlayer];

    [self addGesture];

    

    [self startTimer];

}

– (void)createRoadCanvas

{

    roadCanvas = [[UIView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:roadCanvas];

    

    roadCanvas.layer.anchorPoint = CGPointMake(0.5, 1.0);

    roadCanvas.layer.position = CGPointMake(160, self.view.bounds.size.height);

    

    CATransform3D transform = CATransform3DIdentity;

    transform.m34 = 1.0 / – 500.0;

    roadCanvas.layer.transform =  CATransform3DRotate(transform, 0.3 * M_PI, 1.0f, 0.0f, 0.0f);

}

– (void)addRoad:(CGPoint)p

{

    UIView *road = [[UIView alloc] initWithFrame:self.view.bounds];

    road.userInteractionEnabled = NO;

    road.backgroundColor = [self color:3];

    [roadCanvas addSubview:road];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(110, 0)];

    [path addLineToPoint:CGPointMake(110, 568)];

    [path moveToPoint:CGPointMake(220, 0)];

    [path addLineToPoint:CGPointMake(220, 568)];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [self color:2].CGColor;

    sl.lineWidth = 3;

    sl.lineDashPattern = @[@50, @20];

    sl.path = path.CGPath;

    

    [road.layer addSublayer:sl];

    

    road.center = p;

    [UIView animateWithDuration:4.0 * 4.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        road.center = CGPointMake(p.x, p.y + 568 * 4.0);

    } completion:^(BOOL finished) {

        [road removeFromSuperview];

    }];

    

    int num = arc4random() % 9 + 1;

    UILabel *l = [[UILabel alloc] init];

    l.text = [NSString stringWithFormat:@”%d”, num];

    l.backgroundColor = [UIColor clearColor];

    l.textColor = [self color:4];

    l.font = [UIFont boldSystemFontOfSize:80];

    

    l.layer.anchorPoint = CGPointMake(0.5, 1.0);

    l.layer.position = CGPointMake(l.frame.size.width/2.0, l.frame.size.height);

    

    float x[] = {25, 135, 245};

    int index = arc4random() % 3;

    l.center = CGPointMake(x[index], 40);

    [l sizeToFit];

    [road addSubview:l];

}

– (void)createPlayer

{

    player = [[UIView alloc] initWithFrame:CGRectMake(50, 420, 60, 60)];

    player.userInteractionEnabled = NO;

    player.layer.cornerRadius = 30;

    player.backgroundColor = [self color:1];

    [self.view addSubview:player];

    

    CABasicAnimation *jump = [CABasicAnimation animationWithKeyPath:@”transform.translation.y”];

    jump.toValue = @10;

    jump.duration = 0.5;

    jump.autoreverses = YES;

    jump.repeatCount = HUGE_VAL;

    [player.layer addAnimation:jump forKey:nil];

}

– (void)addGesture

{

    UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(right)];

    right.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:right];

    

    UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(left)];

    left.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:left];

    

}

– (void)right

{

    if (player.tag < 2) {

        player.tag = player.tag + 1;

        [UIView animateWithDuration:0.3 animations:^{

            player.center = CGPointMake(player.center.x + 260.0/3.0, player.center.y);

        }];

    }

}

– (void)left

{

    if (player.tag > 0) {

        player.tag = player.tag1;

        [UIView animateWithDuration:0.3 animations:^{

            player.center = CGPointMake(player.center.x260.0/3.0, player.center.y);

        }];

    }

}

– (void)startTimer

{

    timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(tickA:) userInfo:nil repeats:YES];

}

– (void)tickA:(NSTimer*)sender

{

    [self addRoad:CGPointMake(160, –568.0 * 2.5)];

}

#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(0x1A1A1C);

        case 1:

            return UIColorHex(0x9ED40D);

        case 2:

            return UIColorHex(0xE8EBE1);

        case 3:

            return UIColorHex(0x6D6E77);

        case 4:

            return UIColorHex(0x00D6DD);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end