iPhone アクアリウムで数え方

すうじの6と9は、ひっくり返っているだけなので、どっちがどっちか間違えちゃう。そんな子供に金魚分けゲーム。金魚をタッチして、左右の水槽が6匹、9匹になるように金魚をジャンプさせるiPhoneアプリを作ってみましょう。


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

ポイント
金魚をタッチしたら、CAKeyFrameAnimationをつかって、UIBezierPathで書いた放物線上に金魚が飛んでいくようにしました。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () <UIGestureRecognizerDelegate>{

    CGPoint gridp[9];

    int count;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createBackground];

    

    [self showFish];

    

    [self createGridPoint];

}

– (void)createBackground

{

    UIImage *img = [UIImage imageNamed:@”aquarium”];

    UIImageView *backimg = [[UIImageView alloc] initWithImage:img];

    backimg.frame = CGRectMake(0, 0, 568, 320);

    [self.view addSubview:backimg];

}

– (void)showFish

{

    UIImage *image = [UIImage imageNamed:@”fish”];

    

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

        

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

        float y = (i / 5) * 32 + 170;

        

        UIImageView *fishiv = [[UIImageView alloc] initWithImage:image];

        fishiv.frame = CGRectMake(x, y, 35, 35);

        [self.view addSubview:fishiv];

        

        fishiv.userInteractionEnabled = YES;

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

        [fishiv addGestureRecognizer:tap];

        tap.delegate = self;

    }

}

– (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    if ([touch tapCount] == 1) {

        return YES;

    }

    return NO;

}

– (void)jump:(UITapGestureRecognizer*)gr

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:gr.view.center];

    CGPoint cp = CGPointMake((gridp[count].x + gr.view.center.x) / 2.0, 0);

    [path addQuadCurveToPoint:gridp[count] controlPoint:cp];

    

    CAKeyframeAnimation *j = [CAKeyframeAnimation animationWithKeyPath:@”position”];

    j.path = path.CGPath;

    j.duration = 2;

    gr.view.center = gridp[count];

    [gr.view.layer addAnimation:j forKey:nil];

    

    count++;

    

    // check clear

    if (count == 9) {

        UILabel *clearLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 368, 100)];

        clearLabel.text = @”666666666666\nClear\n99999999999″;

        clearLabel.numberOfLines = 0;

        clearLabel.font = [UIFont boldSystemFontOfSize:20];

        clearLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

        clearLabel.textAlignment = NSTextAlignmentCenter;

        clearLabel.textColor = [UIColor whiteColor];

        [self.view addSubview:clearLabel];

        clearLabel.transform = CGAffineTransformMakeTranslation(0, –500);

        

        [UIView animateWithDuration:0.5 animations:^{

            clearLabel.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            //restart

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

        }];

    }

}

– (void)createGridPoint

{

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

        float x = (i % 3) * 60 + 350;

        float y = (i / 3) * 35 + 200;

        gridp[i] = CGPointMake(x, y);

    }

}

– (void)restart

{

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

        [v removeFromSuperview];

    }

    

    count = 0;

    

    [self createBackground];

    [self showFish];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end