はこを一つえらんで、タッチしてみよう
すうじが隠れているかも!
という感じの子供向けiPhoneゲームのサンプルコード

ポイント
数字のUILabelをinsertSubview:belowSubviewを使って
箱の後ろに隠しています。

環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています

iPhone かくれんぼ サンプルコード

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *hide;

    UILabel *num;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor brownColor];

    

    [self createBoxes];

    

    [self hideItem];

}

– (void)createBoxes

{

    

    CGPoint points[] = {

        {100,100},{220,100},

        {160,250},

        {100,400},{220,400},

    };

    

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

        UIView *box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];

        box.backgroundColor = [UIColor yellowColor];

        box.layer.cornerRadius = 5;

        box.backgroundColor = [UIColor colorWithHue:0.12 *i + 0.2 saturation:1 brightness:1 alpha:1];

        box.center = points[i];

        box.tag = i + 1;

        [self.view addSubview:box];

        

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

        [box addGestureRecognizer:tap];

    }

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    [UIView animateWithDuration:0.3 animations:^{

        gr.view.transform = CGAffineTransformMakeTranslation(0, –50);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{

            gr.view.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            

            if (gr.view == hide) {

                [UIView animateWithDuration:0.2 animations:^{

                    num.transform = CGAffineTransformMakeTranslation(-300, 0);

                } completion:^(BOOL finished) {

                    [num removeFromSuperview];

                    [self hideItem];

                }];

            }

            

        }];

    }];

}

– (void)hideItem

{

    int no = arc4random() % 5 + 1;

    num = [[UILabel alloc] init];

    num.text = [NSString stringWithFormat:@”%d”, no];

    num.font = [UIFont fontWithName:@”Marker Felt” size:30];

    num.backgroundColor = [UIColor clearColor];

    [num sizeToFit];

    

    UIView *box = [self.view viewWithTag:no];

    [self.view insertSubview:num belowSubview:box];

    

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

    shake.toValue = [NSNumber numberWithFloat:-2.0];

    shake.repeatCount = 2;

    shake.duration = 0.3;

    shake.timeOffset = 1;

    shake.autoreverses = YES;

    [box.layer addAnimation:shake forKey:@”shake”];

    

    num.center = box.center;

    hide = box;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end