iPhone かたちの勉強 片付けアプリ

わくの中に、白くて丸いボールと、白くて四角いブロックがでてくるよ。大きい、丸と四角があるからそこにきれいに片付けてみよう。という感じの簡単なiPhoneアプリゲームの作り方を書いてみます。



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

ポイント
2種類のかたち、丸いボールと四角いブロックをUIViewで用意します。まずは大きく、片付けエリアを作成。そのあと、画面の下の方に、小さいUIViewを2種類からランダムで5個表示するようにしました。小さい方にはUIPangestureRecognizerをはめて、指で片付けられるようにしています。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#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]

@interface ViewController () {

    UIView *blockArea;

    UIView *ballArea;

    NSMutableArray *toys;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    

    [self createArea];

    

    [self createPlayground];

    

    [self createToys];

}

– (void)createArea

{

    float x = 568.0 / 4.0;

    blockArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];

    blockArea.backgroundColor = [self color:2];

    blockArea.center = CGPointMake(x, 90);

    blockArea.layer.cornerRadius = 16;

    [self.view addSubview:blockArea];

    

    ballArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];

    ballArea.backgroundColor = [self color:2];

    ballArea.center = CGPointMake(x * 3, 90);

    ballArea.layer.cornerRadius = 80;

    [self.view addSubview:ballArea];

}

– (void)createPlayground

{

    float w = 568.060;

    float h = w / 4.8;

    UIView *playground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];

    playground.center = CGPointMake(568.0/2.0, 240);

    playground.backgroundColor = [UIColor clearColor];

    playground.layer.borderWidth = 2;

    playground.layer.borderColor = [self color:3].CGColor;

    playground.layer.cornerRadius = h * 0.1;

    [self.view addSubview:playground];

    

}

– (void)createToys

{

    toys = [[NSMutableArray alloc] init];

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

        UIView *toy = arc4random() % 2 ? [self createBlock] : [self createBall];

        toy.backgroundColor = [self color:4];

        toy.center = CGPointMake(i * 100 + 84, 250);

        [self.view addSubview:toy];

        

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

        [toy addGestureRecognizer:pan];

        

        [toys addObject:toy];

    }

}

– (UIView*)createBlock

{

    UIView *block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    block.layer.cornerRadius = 5;

    [self.view addSubview:block];

    

    block.tag = 1;

    return block;

}

– (UIView*)createBall

{

    UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    ball.layer.cornerRadius = 25;

    [self.view addSubview:ball];

    

    ball.tag = 2;

    return ball;

}

– (void)move:(UIPanGestureRecognizer*)gr

{

    gr.view.center = [gr locationInView:self.view];

    

    if (gr.state == UIGestureRecognizerStateEnded) {

        // check cleanup

        BOOL finish = YES;

        for (UIView *v in toys) {

            if (v.tag == 1) {

                // block

                if (!CGRectContainsRect(blockArea.frame, v.frame)) {

                    finish = NO;

                }

            } else if (v.tag == 2) {

                // ball

                if (!CGRectContainsRect(ballArea.frame, v.frame)) {

                    finish = NO;

                }

            }

        }

        // next

        if (finish) {

            for (UIView *v in toys) {

                [UIView animateWithDuration:0.4 animations:^{

                    v.alpha = 0;

                } completion:^(BOOL finished) {

                    [v removeFromSuperview];

                }];

            }

            [self performSelector:@selector(createToys) withObject:nil afterDelay:0.5];

        }

        

    }

}

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0x08A689);

        case 1:

            return UIColorHex(0x82BF56);

        case 2:

            return UIColorHex(0xC7D93D);

        case 3:

            return UIColorHex(0xE9F2A0);

        case 4:

            return UIColorHex(0xF2F2F2);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end