ボタンをタッチするまでの時間を表示するだけのゲーム

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

ポイント

・時間に関するところは、NSDate を利用

・Viewのtagに配列のデータ格納場所を入れることで簡易マッピング

・画面上部に経過時間を表示

・反射神経が測定できたらいいな

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    NSMutableArray *targets;

    CADisplayLink *timer;

    UILabel *timeLabel;

    BOOL next;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 表示した時間の保持用に配列を初期化

    targets = [[NSMutableArray alloc] init];

    

    // タイムラベル

    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];

    timeLabel.text = @”00.00″;

    timeLabel.textAlignment = 1; // center

    timeLabel.textColor = [UIColor whiteColor];

    timeLabel.backgroundColor = [UIColor darkGrayColor];

    timeLabel.layer.borderColor = [UIColor blackColor].CGColor;

    timeLabel.layer.borderWidth = 3.0;

    timeLabel.center = CGPointMake(160, 40);

    [self.view addSubview:timeLabel];

    

    // タイマースタート

    [self start];

    

    // 開始

    next = YES;

}

– (void)randomCreate

{

    float x = arc4random() % 220 + 50;

    float y = arc4random() % 240 + 160;

    

    UIView *target = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    target.backgroundColor = [UIColor greenColor];

    target.center = CGPointMake(x, y);

    target.layer.borderWidth = 2.0;

    target.layer.borderColor = [UIColor yellowColor].CGColor;

    target.layer.cornerRadius = 5.0;

    [self.view addSubview:target];

    

    // 開始時間を設定して viewtagに配列のどこに入れたかを残しておく

    id startTime = [NSDate date];

    [targets addObject:startTime];

    target.tag = [targets count];

    

    // タップジェスチャー

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    [target addGestureRecognizer:tap];

}

– (void)tap:(UIGestureRecognizer*)gr

{

    // 表示してからタッチするまでの時間を計算する

    NSDate *startTime = [targets objectAtIndex:gr.view.tag1];

    NSTimeInterval didTouch = [[NSDate date] timeIntervalSinceDate:startTime];

    

    // タッチまでにかかった時間を表示しておく

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

    didit.text = [NSString stringWithFormat:@”%.2f”, didTouch];

    [didit sizeToFit];

    didit.center = gr.view.center;

    didit.backgroundColor = [UIColor clearColor];

    [self.view addSubview:didit];

    

    // タッチしたViewは薄くしておく

    gr.view.alpha = 0.1;

    

    // gestureを消す

    [gr.view removeGestureRecognizer:gr];

    

    // 次を出せるようにする

    next = YES;

}

– (void)start

{

    timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];

    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

– (void)tick:(CADisplayLink*)sender

{

    // ランダムなタイミングで作る

    BOOL create = arc4random() % 100 == 1;

    if (create && next) {

        next = NO; // タップされるまで次を出さない

        [self randomCreate];

    }

    if (!next) {

        // タイマーラベルの時間をガリガリ進める

        NSDate *startTime = [targets lastObject];

        NSTimeInterval diff = [[NSDate date] timeIntervalSinceDate:startTime];

        timeLabel.text = [NSString stringWithFormat:@”%.2f”, diff];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end