黄色い四角をタッチすると、ちょっとだけジャンプするよ
赤いボールにぶつかる前に、タッチして上手くとびこえよう。
というシンプルなサンプルを作ってみた。

ポイント
ジャンプはTapGestureをトリガーにして、
UIViewのanimationWithDurationで上に飛ばします。
アニメーション中の座標は、UIViewオブジェクトからは
とれないので、presentationLayerから取得しています。

環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。


サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    NSTimer *timer;

    UIView *player;

    UILabel *eye;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createBackground];

    

    [self createPlayer];

    

    [self startTimer];

}

– (void)createBackground

{

    self.view.backgroundColor = [UIColor blueColor];

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

    back2.backgroundColor = [UIColor brownColor];

    [self.view addSubview:back2];

}

– (void)createPlayer

{

    player = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    player.backgroundColor = [UIColor yellowColor];

    player.center = CGPointMake(100, 340);

    player.layer.cornerRadius = 5;

    

    eye = [[UILabel alloc] init];

    eye.text = @”●”;

    eye.font = [UIFont boldSystemFontOfSize:20];

    eye.backgroundColor = [UIColor clearColor];

    [eye sizeToFit];

    eye.center = CGPointMake(30, 10);

    [player addSubview:eye];

    

    [self.view addSubview:player];

    

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

    [player addGestureRecognizer:tap];

}

– (void)jump:(UITapGestureRecognizer*)gr

{

    [UIView animateWithDuration:1.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        gr.view.center = CGPointMake(gr.view.center.x, gr.view.center.y50);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1.2 animations:^{

            gr.view.center = CGPointMake(gr.view.center.x, gr.view.center.y + 50);

        }];

    }];

}

– (void)startTimer

{

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];

}

– (void)tick:(NSTimer*)sender

{

    static float time;

    static float nextTime;

    

    time += sender.timeInterval;

    if (time > nextTime) {

        time = 0;

        [self createFire];

        nextTime = 0.1 * (float)(arc4random() % 30) + 2;

    }

    

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

        if (v.tag == 1) { // tag == 1 -> fire

            v.center = CGPointMake(v.center.x1, v.center.y);

            

            if ([eye.text isEqual:@”×”]) {

                continue;

            }

            

            CGRect currentRect = [player.layer.presentationLayer frame];

            if (CGRectIntersectsRect(v.frame, currentRect)) {

                UILabel *l = [player.subviews objectAtIndex:0];

                l.text = @”×”;

                [UIView animateWithDuration:0.4 animations:^{

                    player.alpha = 0.8;

                } completion:^(BOOL finished) {

                    player.alpha = 1.0;

                    l.text = @”●”;

                }];

            }

        }

    }

}

– (void)createFire

{

    UIView *fire = [[UIView alloc] initWithFrame:CGRectMake(330, 340, 20, 20)];

    fire.backgroundColor = [UIColor redColor];

    fire.layer.cornerRadius = 10;

    [self.view addSubview:fire];

    

    fire.tag = 1;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end