iPhone色アプリ

4色の丸を使っての神経衰弱ゲームをiPhoneアプリとして描いてみます。


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

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (strong, nonatomic) NSMutableArray *colors;

@property (weak, nonatomic) UIView *first;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self createChips];

}

– (void)createChips

{

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

        float x = (i % 5) * 60 + 15;

        float y = (i / 5) * 60 + 50;

        UIView *chip = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];

        chip.backgroundColor = [UIColor whiteColor];

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

        chip.layer.borderWidth = 1;

        chip.layer.cornerRadius = 25;

        chip.tag = i;

        [self.view addSubview:chip];

        

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

        [chip addGestureRecognizer:tap];

    }

}

– (void)flip:(UIGestureRecognizer*)gr

{

    // check

    UIView *reservedView;

    if (!self.first) {

        self.first = gr.view;

    } else {

        reservedView = self.first;

        self.first = nil;

    }

    

    [UIView animateWithDuration:0.2 animations:^{

        gr.view.layer.transform = CATransform3DMakeRotation(M_PI/2.0, 1, 0, 0);

    } completion:^(BOOL finished) {

        gr.view.backgroundColor = [self.colors objectAtIndex:gr.view.tag];

        [UIView animateWithDuration:0.2 animations:^{

            gr.view.layer.transform = CATransform3DIdentity;

            

            if (reservedView) {

                if (![reservedView.backgroundColor isEqual:gr.view.backgroundColor]) {

                    // ng

                    [self performSelector:@selector(reverse:) withObject:@[reservedView, gr.view] afterDelay:0.5];

                } else {

                    // ok

                    reservedView.userInteractionEnabled = NO;

                    gr.view.userInteractionEnabled = NO;

                }

            }

        }];

    }];

}

– (void)reverse:(NSArray*)arr

{

    UIView *one = arr[0];

    UIView *two = arr[1];

    [UIView animateWithDuration:0.2 animations:^{

        one.layer.transform = CATransform3DMakeRotation(M_PI/2.0, 1, 0, 0);

        two.layer.transform = CATransform3DMakeRotation(M_PI/2.0, 1, 0, 0);

    } completion:^(BOOL finished) {

        one.backgroundColor = [UIColor whiteColor];

        two.backgroundColor = [UIColor whiteColor];

        [UIView animateWithDuration:0.2 animations:^{

            one.layer.transform = CATransform3DIdentity;

            two.layer.transform = CATransform3DIdentity;

        }];

    }];

}

– (NSMutableArray*)colors

{

    if (!_colors) {

        _colors = [[NSMutableArray alloc] init];

        

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

            [_colors addObject:[self color:i%5]];

        }

        

        // shuffle

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

            int nElements = 40 – i;

            int n = (random() % nElements) + i;

            [_colors exchangeObjectAtIndex:i withObjectAtIndex:n];

        }

    }

    return _colors;

}

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

        case 1:

            return UIColorHex(0x782E42);

        case 2:

            return UIColorHex(0xFF745F);

        case 3:

            return UIColorHex(0xFFFE94);

        case 4:

            return UIColorHex(0xCAFF85);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end