真ん中の穴とおなじ形のひよこはどれかな?
みつかったら、穴のとこまでひよこを連れて行ってね!
という感じの子供向けiPhone ゲームのサンプルコードを書いてみた。

ポイント
ひよこの形をA,B,C,Dと四種類用意して、
真ん中は、黒でランダムABCDから表示してみました。
黒とひよこが接触したタイミングで一致判定をしています。

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

iPhone サンプルかたち合わせ ひよこ

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *shadow;

    BOOL loading;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    [self setUpHiyoko];

    [self showShadow];

}

– (void)setUpHiyoko

{

    UIView *a = [self createHiyokoA];

    a.center = CGPointMake(50, 350);

    [self addFace:a];

    [self.view addSubview:a];

    [self addPanGesture:a];

    

    UIView *b = [self createHiyokoB];

    b.center = CGPointMake(120, 350);

    [self addFace:b];

    [self.view addSubview:b];

    [self addPanGesture:b];

    

    UIView *c = [self createHiyokoC];

    c.center = CGPointMake(190, 350);

    [self addFace:c];

    [self.view addSubview:c];

    [self addPanGesture:c];

    

    UIView *d = [self createHiyokoD];

    d.center = CGPointMake(260, 350);

    [self addFace:d];

    [self.view addSubview:d];

    [self addPanGesture:d];

}

– (UIView*)createHiyokoA

{

    UIView *hiyoko = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    hiyoko.backgroundColor = [UIColor yellowColor];

    hiyoko.layer.cornerRadius = 10;

    hiyoko.tag = 1;

    return hiyoko;

}

– (UIView*)createHiyokoB

{

    UIView *hiyoko = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    hiyoko.backgroundColor = [UIColor yellowColor];

    hiyoko.layer.cornerRadius = 14;

    hiyoko.tag = 2;

    return hiyoko;

}

– (UIView*)createHiyokoC

{

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

    hiyoko.backgroundColor = [UIColor yellowColor];

    hiyoko.layer.cornerRadius = 8;

    hiyoko.tag = 3;

    return hiyoko;

}

– (UIView*)createHiyokoD

{

    UIView *hiyoko = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    hiyoko.backgroundColor = [UIColor clearColor];

    hiyoko.tag = 4;

    

    // round corner triangle

    UIBezierPath *path = [UIBezierPath bezierPath];

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

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

    [path addQuadCurveToPoint:CGPointMake(30, 4) controlPoint:CGPointMake(30, 0)];

    [path addLineToPoint:CGPointMake(17, 28)];

    [path addQuadCurveToPoint:CGPointMake(13, 28) controlPoint:CGPointMake(15, 30)];

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

    [path addQuadCurveToPoint:CGPointMake(4, 0) controlPoint:CGPointMake(0, 0)];

    

    CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:hiyoko.layer];

    sl.fillColor = [UIColor yellowColor].CGColor;

    sl.path = path.CGPath;

    

    [hiyoko.layer addSublayer:sl];

    

    return hiyoko;

}

– (void)addPanGesture:(UIView*)v

{

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

    [v addGestureRecognizer:pan];

}

– (void)pan:(UIPanGestureRecognizer*)gr

{

    if (loading) {

        return;

    }

    

    [self.view insertSubview:gr.view aboveSubview:shadow];

    

    CGPoint p = [gr locationInView:self.view];

    gr.view.center = p;

    

    if (CGRectIntersectsRect(gr.view.frame, shadow.frame)) {

        if (gr.view.tag == shadow.tag) {

            loading = YES;

            [UIView animateWithDuration:0.5 animations:^{

                gr.view.center = shadow.center;

            } completion:^(BOOL finished) {

                [self clear];

            }];

        }

    }

}

– (void)showShadow

{

    int type = arc4random() % 4;

    switch (type) {

        case 0: shadow = [self createHiyokoA]; break;

        case 1: shadow = [self createHiyokoB]; break;

        case 2: shadow = [self createHiyokoC]; break;

        case 3: shadow = [self createHiyokoD]; break;

        default:

            break;

    }

    shadow.center = CGPointMake(160, 150);

    

    if (type == 3) {

        CAShapeLayer *l = [shadow.layer.sublayers objectAtIndex:0];

        l.fillColor = [UIColor blackColor].CGColor;

    } else {

        shadow.backgroundColor = [UIColor blackColor];

    }

    

    [self.view addSubview:shadow];

}

– (void)addFace:(UIView*)hiyoko

{

    UIView *eyeR = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 3, 3)];

    eyeR.layer.cornerRadius = 1.5;

    eyeR.backgroundColor = [UIColor blackColor];

    

    

    UIView *eyeL = [[UIView alloc] initWithFrame:CGRectMake(20, 5, 3, 3)];

    eyeL.layer.cornerRadius = 1.5;

    eyeL.backgroundColor = [UIColor blackColor];

    

    UIView *mouth = [[UIView alloc] initWithFrame:CGRectMake(11, 12, 8, 3)];

    mouth.layer.cornerRadius = 1.5;

    mouth.backgroundColor = [UIColor orangeColor];

    

    [hiyoko addSubview:eyeR];

    [hiyoko addSubview:eyeL];

    [hiyoko addSubview:mouth];

    

}

– (void)clear

{

    UILabel *clear = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 150)];

    clear.text = @”OK”;

    clear.font = [UIFont boldSystemFontOfSize:40];

    clear.textAlignment = 1;

    clear.textColor = [UIColor yellowColor];

    clear.backgroundColor = [UIColor orangeColor];

    clear.layer.cornerRadius = 20;

    clear.alpha = 0.8;

    [self.view addSubview:clear];

    clear.transform = CGAffineTransformMakeTranslation(0, –300);

    

    [UIView animateWithDuration:0.5 animations:^{

        clear.transform = CGAffineTransformIdentity;

    } completion:^(BOOL finished) {

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

            clear.transform = CGAffineTransformMakeTranslation(0, –300);

        } completion:^(BOOL finished) {

            

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

                [v removeFromSuperview];

            }

            [self setUpHiyoko];

            [self showShadow];

            loading = NO;

        }];

    }];

    

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end