iPhone色とかず

一番多いと思う色を下のパネルから選んでみよう。ぱっとみて、「多い」、「少ない」を判断する能力を鍛えるiPhoneアプリを作ってみます。


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

ポイント
格子状に白丸を配置して、それにたいしてループでランダムに色を設定しています。ループの中で、それぞれの色がなんこ表示されたかを数えておいて、一番多いものがどれかをチェックしています。画面下には回答用にパネルを配置、UITapGestureをはめておいて、タップのタイミングで答え合わせをします。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#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]

@interface ViewController () {

    NSMutableArray *pins;

    int answer;

    

    UIView *ngMark;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:0];

    

    [self createPanels];

    

    [self createPins];

    

    [self startGame];

}

– (void)createPanels

{

    

    float w = 151;

    float h = w / 1.618;

    

    UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height2*h – 18, 320, 200)];

    back.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:back];

    

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

        float x = (i%2) * (w+6) + 6;

        float y = (i/2) * (h + 6) + self.view.bounds.size.height2*h – 12;

        

        UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];

        panel.backgroundColor = [self color:i + 1];

        [self.view addSubview:panel];

        

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

        [panel addGestureRecognizer:tap];

    }

}

– (void)createPins

{

    pins = [[NSMutableArray alloc] init];

    

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

        float x = (i%10) * 30 + 10;

        float y = (i/10) * 30 + 10;

        

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

        pin.center = CGPointMake(x + 15, y + 15);

        pin.backgroundColor = [UIColor whiteColor];

        pin.layer.cornerRadius = 10;

        [self.view addSubview:pin];

        

        [pins addObject:pin];

    }

}

– (void)startGame

{

    // 色の出現回数をカウント

    int a = 0;

    int b = 0;

    int c = 0;

    int d = 0;

    

    for (UIView *pin in pins) {

        int i = arc4random() % 6 + 1;

        if (i < 5) {

            pin.backgroundColor = [self color:i];

            pin.tag = i;

        } else {

            pin.tag = 6;

        }

        

        

        switch (i) {

            case 1: a++; break;

            case 2: b++; break;

            case 3: c++; break;

            case 4: d++; break;

        }

    }

    

    NSArray *numbers = @[@(a), @(b), @(c), @(d)];

    answer = [numbers indexOfObject:[numbers valueForKeyPath:@”@max.intValue”]] + 1;

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    if ([gr.view.backgroundColor isEqual:[self color:answer]]) {

        // ok

        UIView *ok = [[UIView alloc] initWithFrame:CGRectMake(60, 60, 200, 200)];

        ok.layer.cornerRadius = 100;

        ok.layer.borderColor = [UIColor blueColor].CGColor;

        ok.layer.borderWidth = 20;

        ok.backgroundColor = [UIColor clearColor];

        [self.view addSubview:ok];

        

        ok.alpha = 0;

        [UIView animateWithDuration:0.5 animations:^{

            ok.alpha = 1.0;

        } completion:^(BOOL finished) {

            [ok removeFromSuperview];

            

            [self leveling];

            

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

        }];

        

    }

    else

    {

        // ng

        if (!ngMark) {

            ngMark = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];

            UIBezierPath *ngpath = [UIBezierPath bezierPath];

            [ngpath moveToPoint:CGPointMake(10, 10)];

            [ngpath addLineToPoint:CGPointMake(300, 300)];

            [ngpath moveToPoint:CGPointMake(10, 300)];

            [ngpath addLineToPoint:CGPointMake(300, 10)];

            CAShapeLayer *sl = [[CAShapeLayer alloc] init];

            sl.fillColor = [UIColor clearColor].CGColor;

            sl.strokeColor = [UIColor redColor].CGColor;

            sl.lineWidth = 20;

            sl.path = ngpath.CGPath;

            [ngMark.layer addSublayer:sl];

            [self.view addSubview:ngMark];

            ngMark.alpha = 0;

        }

        

        [UIView animateWithDuration:0.5 animations:^{

            ngMark.alpha = 1.0;

        } completion:^(BOOL finished) {

            ngMark.alpha = 0;

        }];

    }

}

– (void)leveling

{

    // 4色を整地する

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

        int counter = 0;

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

            if (pin.tag == i) {

                float x = (counter % 10) * 20 + 50;

                float y = (counter / 10) * 20 + 30 + (i – 1) * 70;

                [UIView animateWithDuration:2.0 animations:^{

                    pin.center = CGPointMake(x, y);

                }];

                counter ++;

            } else if (pin.tag == 6) {

                pin.backgroundColor = [UIColor clearColor];

            }

        }

    }

}

– (void)restart

{

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

        [v removeFromSuperview];

    }

    ngMark = nil;

    

    

    [self createPanels];

    

    [self createPins];

    

    [self startGame];

}

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xA8A373);

        case 1:

            return UIColorHex(0xA3DBE8);

        case 2:

            return UIColorHex(0xD2FFC0);

        case 3:

            return UIColorHex(0xDDB3FF);

        case 4:

            return UIColorHex(0xFFBFC1);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end