指瓦割りのアプリ

(XcodeのiOS6 iPhone Simulatorで動かしています。)

概要

画面をなぞる速さで、正拳突きの威力を決め

それに応じた数の瓦が割れてみれるような、変なゲームを

作ってみようと思う。

ポイント

・widthが半分の瓦Viewのコピーを使って割れたように見せる

・UIPanGestureRecognizerのvelocityInViewのy成分から速さを決定

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *goo;

    UIView *guide;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    goo = [self createGoo];

    

    [self createKawara];

    

    [self showGuide];

    

    [self createButton];

    

}

– (UIView*)createGoo

{

    UIView *hand = [[UIView alloc] initWithFrame:CGRectMake(110, 20, 100, 100)];

    [self.view addSubview:hand];

    

    UIView *palm = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 100, 90)];

    palm.backgroundColor = [UIColor yellowColor];

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

    palm.layer.borderWidth = 3.0;

    palm.layer.cornerRadius = 20;

    [hand addSubview:palm];

    

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

        float x = 22.5 * i;

        UIView *finger = [[UIView alloc] initWithFrame:CGRectMake(x+5, 0, 24, 50)];

        finger.backgroundColor = [UIColor yellowColor];

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

        finger.layer.borderWidth = 3.0;

        finger.layer.cornerRadius = 5.0;

        [hand addSubview:finger];

    }

    

    UIView *thum = [[UIView alloc] initWithFrame:CGRectMake(60, 30, 40, 25)];

    thum.backgroundColor = [UIColor yellowColor];

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

    thum.layer.borderWidth = 3.0;

    thum.layer.cornerRadius = 5.0;

    [hand addSubview:thum];

    

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

    memo.text = @”swipe!”;

    memo.font = [UIFont boldSystemFontOfSize:30];

    memo.backgroundColor = [UIColor clearColor];

    [memo sizeToFit];

    memo.center = CGPointMake(50, 70);

    [hand addSubview:memo];

    hand.transform = CGAffineTransformMakeRotation(M_PI);

    

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

    [hand addGestureRecognizer:swipe];

    

    return hand;

}

– (void)swipe:(UIPanGestureRecognizer*)gr

{

    static float velocity;

    

    velocity = MAX([gr velocityInView:self.view].y, velocity);

    

    if (gr.state == UIGestureRecognizerStateEnded) {

        [self breakKawara:velocity];

        velocity = 0;

    }

}

– (void)breakKawara:(float)velocity

{

    int count = MIN(velocity / 200, 20);

    

    [UIView animateWithDuration:0.05 * count animations:^{

        UIView *k = [self.view viewWithTag:count – 1];

        goo.transform = CGAffineTransformTranslate(goo.transform, 0, goo.center.y – k.center.y + 50);

    }];

    

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

        UIView *k = [self.view viewWithTag:i+1];

        UIView *k2 = [[UIView alloc] initWithFrame:k.frame];

        

        k.frame = CGRectMake(k.frame.origin.x, k.frame.origin.y,k.frame.size.width/2.0, k.frame.size.height);

        

        float x2 = k.frame.origin.x + k.frame.size.width;

        float y2 = k.frame.origin.y;

        k2.frame = CGRectMake(x2, y2, k.frame.size.width, k.bounds.size.height);

        k2.backgroundColor = k.backgroundColor;

        k2.layer.borderWidth = k.layer.borderWidth;

        k2.layer.borderColor = k.layer.borderColor;

        k2.tag = 100;

        [self.view addSubview:k2];

        

        [UIView animateWithDuration:0.5 delay:0.05 * i options:UIViewAnimationOptionCurveEaseIn animations:^{

            k.transform = CGAffineTransformMakeRotation(M_PI*0.2);

            k2.transform = CGAffineTransformMakeRotation(-M_PI*0.2);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:.2 animations:^{

                k.transform = CGAffineTransformTranslate(k.transform, –300, –40);

                k2.transform = CGAffineTransformTranslate(k2.transform, 300, –40);

            }];

        }];

    }

}

– (void)createKawara

{

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

        float x = 85;

        float y = 12 * i + 220;

        UIView *kawara = [[UIView alloc] initWithFrame:CGRectMake(x, y, 140, 10)];

        kawara.backgroundColor = [UIColor darkGrayColor];

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

        kawara.layer.borderWidth = 2;

        kawara.tag = i+1;

        [self.view addSubview:kawara];

    }

}

– (void)showGuide

{

    guide = [self createGoo];

    guide.alpha = 0.3;

    guide.center = goo.center;

    

    float startGhost = guide.center.y;

    

    CABasicAnimation *down = [CABasicAnimation animationWithKeyPath:@”transform.translation.y”];

    down.fromValue = [NSNumber numberWithDouble:startGhost];

    down.toValue = [NSNumber numberWithDouble:200];

    down.repeatCount = 3;

    down.duration = 1.0;

    down.fillMode = kCAFillModeForwards;

    down.removedOnCompletion = NO;

    down.delegate = self;

    [guide.layer addAnimation:down forKey:@”donw hand”];

}

– (void)createButton

{

    UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];

    btn.layer.cornerRadius = 25;

    btn.backgroundColor = [UIColor redColor];

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

    btn.layer.borderWidth = 2;

    [self.view addSubview:btn];

    

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

    [btn addGestureRecognizer:tap];

}

– (void)reset

{

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

        [UIView animateWithDuration:1 animations:^{

            v.transform = CGAffineTransformIdentity;

        }];

    }

    [UIView animateWithDuration:2 animations:^{

        goo.transform = CGAffineTransformMakeRotation(M_PI);

     

    } completion:^(BOOL finished) {

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

            if (v.tag == 100) {

                [v removeFromSuperview];

            } else if (v.tag > 0) {

                v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, 140, 10);

            }

        }

    }];

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

    [guide removeFromSuperview];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end