iPhone光る順番

光った順番を覚えてボタンをタップしていくiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *positions;

@property int8_t state;

@end

#define GameStateTap 1

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    

    [self createRandom];

    [self createLight];

    [self createButton];

}

– (void)createRandom

{

    NSMutableArray *seed = [@[@(1), @(2), @(3), @(4), @(5), @(6)] mutableCopy];

    NSUInteger count = seed.count;

    self.positions = [[NSMutableArray alloc] initWithCapacity:count];

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

        NSUInteger idx = arc4random() % seed.count;

        [self.positions addObject:seed[idx]];

        [seed removeObjectAtIndex:idx];

    }

}

– (void)createLight

{

    CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), 200);

    float dAngle = 2.0 * M_PI / 6.0;

    float r = 100;

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

        float x = r * cos(dAngle * i) + o.x;

        float y = r * sin(dAngle * i) + o.y;

        UIView *light = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];

        light.tag = i + 1;

        light.center = CGPointMake(x, y);

        light.backgroundColor = [UIColor colorWithHue:0.15 * (i+1) saturation:0.5 brightness:0.3 alpha:1];

        light.transform = CGAffineTransformMakeRotation(dAngle * i + M_PI_2);

        [self.view addSubview:light];

        

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

        l.text = [self.positions[i] stringValue];

        l.font = [UIFont systemFontOfSize:50];

        l.textColor = [light.backgroundColor colorWithAlphaComponent:0.3];

        [l sizeToFit];

        l.center = [light convertPoint:light.center fromView:self.view];

        [light addSubview:l];

        

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

        [light addGestureRecognizer:tap];

    }

}

– (void)createButton

{

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    start.titleLabel.font = [UIFont boldSystemFontOfSize:40];

    [start setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [start setTitle:@”start!” forState:UIControlStateNormal];

    [start sizeToFit];

    start.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMaxY(self.view.frame) – 150);

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];

}

– (void)start:(UIButton *)sender

{

    [UIView transitionWithView:sender duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{

        [sender setTitle:@”look!” forState:UIControlStateNormal];

    } completion:nil];

    

    for (int i=0; i < self.positions.count; i++) {

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            

            [self lighting:[self.view viewWithTag:[self.positions indexOfObject:@(i+1)] + 1]];

        });

    }

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [UIView transitionWithView:sender duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{

            [sender setTitle:@”tap!” forState:UIControlStateNormal];

        } completion:^(BOOL finished) {

            self.state = GameStateTap;

        }];

    });

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    if (self.state == GameStateTap) {

        [self lighting:gr.view];

    }

}

– (void)lighting:(UIView *)view

{

    [UIView animateWithDuration:0.2 animations:^{

        view.layer.backgroundColor = [UIColor colorWithHue:0.15 * view.tag saturation:0.8 brightness:1.0 alpha:1].CGColor;

    } completion:^(BOOL finished) {

        

        id obj = [self.positions valueForKeyPath:@”@min.self”];

        UILabel *l = (UILabel*)view.subviews[0];

        if (self.state == GameStateTap && [[obj stringValue] isEqual:l.text] ) {

            // correct

            [self.positions removeObject:obj];

            

            if (self.positions.count == 0) {

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

            }

            return;

        }

        

        UIColor *originColor = [UIColor colorWithHue:0.15 * view.tag saturation:0.5 brightness:0.3 alpha:1];

        [UIView animateWithDuration:1.5 animations:^{

            view.layer.backgroundColor = originColor.CGColor;

        } completion:^(BOOL finished) {

            ((UILabel*)view.subviews[0]).textColor = originColor;

        }];

    }];

}

– (void)restart

{

    self.view = [[UIView alloc] initWithFrame:self.view.frame];

    self.view.backgroundColor = [UIColor grayColor];

    self.state = 0;

    [self createRandom];

    [self createLight];

    [self createButton];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

@end