iPhone 手裏剣

同じ色のカラーブロックに手裏剣があたると、ブロックがくるっと回るようなiPhoneアプリを描いてみます。(Xcodeアップデートしたら、結構変わってた。今日からiOS7で描いてます。)


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

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (strong, nonatomic) UIView *shuriken;

@property (strong, nonatomic) NSMutableArray *targets;

@property (nonatomic) CGPoint velocity;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    

    [self createTargets];

    [self createShuriken];

    [self startTimer];

}

– (void)createTargets

{

    if (!self.targets) {

        self.targets = [[NSMutableArray alloc] init];

    }

    

    NSArray *colors = @[ [self color:0], [self color:1], [self color:4]];

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

        

        float x = 60 + i * 100;

        

        UIView *target = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];

        target.backgroundColor = colors[i];

        target.center = CGPointMake(x, 100);

        

        target.layer.shadowColor = [self color:2].CGColor;

        target.layer.shadowOffset = CGSizeMake(0, 0);

        target.layer.shadowOpacity = 0.8;

        target.layer.shadowRadius = 10;

        

        [self.view addSubview:target];

        [self.targets addObject:target];

    }

}

– (void)createShuriken

{

    NSArray *colors = @[ [self color:0], [self color:1], [self color:4]];

    UIColor *color = colors[arc4random()%3];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

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

    [path addLineToPoint:CGPointMake(25, 15)];

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

    [path addLineToPoint:CGPointMake(25, 25)];

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

    [path addLineToPoint:CGPointMake(15, 25)];

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

    [path addLineToPoint:CGPointMake(15, 15)];

    [path closePath];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = color.CGColor;

    sl.path = path.CGPath;

    

    self.shuriken = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    self.shuriken.center = CGPointMake(160, 400);

    [self.shuriken.layer addSublayer:sl];

    [self.view addSubview:self.shuriken];

    

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

    [self.shuriken addGestureRecognizer:pan];

}

– (void)shushushu:(UIPanGestureRecognizer*)gr

{

    self.velocity = [gr velocityInView:self.view];

    float v = hypotf(self.velocity.x, self.velocity.y);

    if (v > 2000) {

        [gr.view removeGestureRecognizer:gr];

    }

}

– (void)startTimer

{

    [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];

}

– (void)tick:(NSTimer*)sender

{

    if (self.shuriken) {

        float x = self.shuriken.center.x + self.velocity.x / 100;

        float y = self.shuriken.center.y + self.velocity.y / 100;

        self.shuriken.center = CGPointMake(x, y);

        if (self.velocity.x != 0 || self.velocity.y !=0) {

            self.shuriken.transform = CGAffineTransformRotate(self.shuriken.transform, M_PI*0.1);

        }

        

        for (UIView *target in self.targets) {

            UIColor *shurikenColor = [UIColor colorWithCGColor:[(CAShapeLayer*)self.shuriken.layer.sublayers[0] fillColor]];

            if ([target.backgroundColor isEqual:shurikenColor]

                && CGRectIntersectsRect(self.shuriken.frame, target.frame)

                && self.shuriken.tag == 0

                ) {

                self.shuriken.tag = 1;

                [UIView transitionWithView:target duration:0.3 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{

                } completion:^(BOOL finished) {

                }];

            }

        }

        

        if (self.shuriken.center.y < 0) {

            self.velocity = CGPointZero;

            [self.shuriken removeFromSuperview];

            [self createShuriken];

        }

    }

}

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

        case 1:

            return UIColorHex(0xCB3B66);

        case 2:

            return UIColorHex(0xF6DD99);

        case 3:

            return UIColorHex(0xABC680);

        case 4:

            return UIColorHex(0x313445);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end