かぶにゆびでさわるとメーターがでてくるよ
ちょうどのところでゆびをはなせれば
かぶがすぽっと、ぬけちゃうよ。
という感じの子供向けiPhoneゲームのサンプルコードを書いてみた。

ポイント
UILongPressGestureRecognizerのBeginとEndをりようして、
レベルゲージのON/OFFを制御しています。
メータのレベルは、NSTimerで一目盛りずつUPさせてます。

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


サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *selected;

    UIView *levelGauge;

    NSTimer *timer;

    int counter;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createTurnips];

    [self coverSand];

}

– (void)createTurnips

{    

    UIImage *turnipImg = [UIImage imageNamed:@”turnip”];

    UIImage *leavesImg = [UIImage imageNamed:@”leaves”];

    CGPoint points[3] = {CGPointMake(0, 300), CGPointMake(100, 300), CGPointMake(200, 300)};

    

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

        CGPoint p = points[i];

        

        UIImageView *turnip = [[UIImageView alloc] initWithImage:turnipImg];

        turnip.frame = CGRectMake(0, 0, 100, 100);

        turnip.backgroundColor = [UIColor clearColor];

        

        UIImageView *leaves = [[UIImageView alloc] initWithImage:leavesImg];

        leaves.frame = CGRectMake(0, 0, 100, 100);

        leaves.backgroundColor = [UIColor clearColor];

        

        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(p.x, p.y, 100, 100)];

        v.backgroundColor = [UIColor clearColor];

        [v addSubview:turnip];

        [v addSubview:leaves];

        [self.view addSubview:v];

        

        v.userInteractionEnabled = YES;

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

        tap.minimumPressDuration = 0.01;

        [v addGestureRecognizer:tap];

    }

}

– (void)tap:(UILongPressGestureRecognizer*)gr

{

    if (gr.state == UIGestureRecognizerStateBegan) {

        [self startGauge:gr.view];

        selected = gr.view;

    } else if (gr.state == UIGestureRecognizerStateEnded) {

        [self stopGauge];

    }

}

– (void)startGauge:(UIView*)turnip

{

    levelGauge = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 100)];

    levelGauge.backgroundColor = [UIColor clearColor];

    levelGauge.layer.borderWidth = 2;

    levelGauge.layer.borderColor = [UIColor darkGrayColor].CGColor;

    [self.view addSubview:levelGauge];

    

    levelGauge.center = CGPointMake(turnip.center.x, turnip.center.y100);

    

    counter = 0;

    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(levelup:) userInfo:[NSNumber numberWithInt:0] repeats:YES];

}

– (void)levelup:(NSTimer*)sender

{

    float y = 8020 * counter;

    

    NSArray *colors = [NSArray arrayWithObjects:

                       [UIColor blueColor],

                       [UIColor blueColor],

                       [UIColor greenColor],

                       [UIColor greenColor],

                       [UIColor redColor],

                       [UIColor redColor], nil];

    

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(1, y, 18, 19)];

    v.backgroundColor = [colors objectAtIndex:counter];

    [levelGauge addSubview:v];

    counter++;

}

– (void)stopGauge

{

    [timer invalidate];

    

    if ([levelGauge.subviews count] < 3) {

        [UIView animateWithDuration:0.2 animations:^{

            selected.transform = CGAffineTransformMakeScale(1.1, 1.2);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.3 animations:^{

                selected.transform = CGAffineTransformIdentity;

            }];

        }];

    } else if ([levelGauge.subviews count] < 5) {

        [UIView animateWithDuration:0.5 animations:^{

            selected.transform = CGAffineTransformMakeTranslation(0, –150);

        }];

    } else {

        [UIView animateWithDuration:0.2 animations:^{

            UIView *leaves = [selected.subviews objectAtIndex:1];

            leaves.transform = CGAffineTransformMakeTranslation(0, –400);

        }];

    }

    

    [levelGauge removeFromSuperview];

}

– (void)coverSand

{

    UIView *sand = [[UIView alloc] initWithFrame:CGRectMake(0, 350, 320, 150)];

    sand.backgroundColor = [UIColor brownColor];

    [self.view addSubview:sand];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end