iPhoneフリック単純ゲーム

画面上でフリックすると矢印が飛ぶだけの、とても単純なiPhoneゲームを描いてみます。真ん中に表示された矢印とおんなじ向きの矢印を飛ばせたら、真ん中の矢印が回転するようにしました。

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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

{

    UIView *centerArrow;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self createBackground];

    

    centerArrow = [self createArrow:[self color:4]];

    [self addSwipeGesture];

}

– (UIView*)createArrow:(UIColor*)color

{

    UIView *arrow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

    arrow.backgroundColor = [self color:3];

    arrow.center = CGPointMake(160, 220);

    [self.view addSubview:arrow];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

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

    [path addLineToPoint:CGPointMake(50, 20)];

    [path addLineToPoint:CGPointMake(50, 0)];

    [path addLineToPoint:CGPointMake(100, 50)];

    [path addLineToPoint:CGPointMake(50, 100)];

    [path addLineToPoint:CGPointMake(50, 80)];

    [path addLineToPoint:CGPointMake(0, 80)];

    [path closePath];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.position = CGPointMake(30, 25);

    sl.fillColor = color.CGColor;

    sl.path = path.CGPath;

    

    [arrow.layer addSublayer:sl];

    return arrow;

}

– (void)addSwipeGesture

{

    UISwipeGestureRecognizerDirection direction[] = {UISwipeGestureRecognizerDirectionDown, UISwipeGestureRecognizerDirectionLeft, UISwipeGestureRecognizerDirectionRight, UISwipeGestureRecognizerDirectionUp};

    

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

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

        s.direction = direction[i];

        [self.view addGestureRecognizer:s];

    }

}

– (void)swipe:(UISwipeGestureRecognizer*)gr

{

    UIView *v = [self createArrow:[self color:2]];

    v.backgroundColor = [UIColor clearColor];

    

    CGPoint start;

    switch (gr.direction) {

        case UISwipeGestureRecognizerDirectionDown:

            start = CGPointMake(160, –200);

            v.transform = CGAffineTransformMakeRotation(M_PI *0.5);

            break;

        case UISwipeGestureRecognizerDirectionLeft:

            start = CGPointMake(520, 220);

            v.transform = CGAffineTransformMakeRotation(M_PI);

            break;

        case UISwipeGestureRecognizerDirectionRight:

            start = CGPointMake(-200, 220);

            break;

        case UISwipeGestureRecognizerDirectionUp:

            start = CGPointMake(160, 800);

            v.transform = CGAffineTransformMakeRotation(-M_PI *0.5);

            break;

        default:

            break;

    }

    

    v.center = start;

    [UIView animateWithDuration:0.3 animations:^{

        v.center = CGPointMake(160, 220);

    } completion:^(BOOL finished) {

        if (CGAffineTransformEqualToTransform(centerArrow.transform, v.transform)) {

            [UIView animateWithDuration:0.3 animations:^{

                v.transform = CGAffineTransformScale(v.transform, 0.2, 0.2);

            } completion:^(BOOL finished) {

                // restart

                [self next];

                [v removeFromSuperview];

            }];

        } else {

            [UIView animateWithDuration:0.2 animations:^{

                v.center = start;

            } completion:^(BOOL finished) {

                [v removeFromSuperview];

            }];

        }

        

    }];

}

– (void)next

{

    [centerArrow removeFromSuperview];

    centerArrow = [self createArrow:[self color:4]];

    

    float randAngle = 0.5 * M_PI * (float)(arc4random() % 4) – 0.5 * M_PI;

    [UIView animateWithDuration:0.2 animations:^{

        centerArrow.transform = CGAffineTransformMakeRotation(randAngle);

    }];

    

}

– (void)createBackground

{

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

        float x = (i % 10) * 32 + 16;

        float y = (i / 10) * 32 + 16;

        UIView *b = [self createArrow:[self color:1]];

        b.alpha = 0.4;

        b.center = CGPointMake(x, y);

        float randAngle = 0.5 * M_PI * (float)(arc4random() % 4) – 0.5 * M_PI;

        b.transform = CGAffineTransformMakeScale(0.2, 0.2);

        b.transform = CGAffineTransformRotate(b.transform, randAngle);

        [self.view addSubview:b];

    }

}

                    

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

        case 1:

            return UIColorHex(0xCED9A7);

        case 2:

            return UIColorHex(0x9CBF50);

        case 3:

            return UIColorHex(0xF2BBC9);

        case 4:

            return UIColorHex(0xF2668B);

        default:

            break;

}

return nil;

}

                    

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end