力を溜めて薪割りをするiPhoneゲームアプリのサンプルです。
画面の右下をタップしたままにすると、薪割りメーターが上がります。
ちょうどいいところで止めて、斧を振り下ろして薪を真っ二つに!

ポイント
pushと書いてある場所を押している長さと、
メーターのレベルを0.2秒で1レベルアップとしました。
touchesBeganとtouchesEndedでON/OFFを行い、
NSTimerの中で増減をさせています。



サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController (){

    UIView *stump;

    UIView *hatchet;

    NSTimer *timer;

    UILabel *btn;

    UIView *wood;

    int powerlevel;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createStump];

    

    [self createHatchet];

    

    [self createFireWood];

    

    [self createUI];

    

    [self startTimer];

}

– (void)createStump

{

    UIView *green = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 200)];

    green.backgroundColor = [UIColor greenColor];

    [self.view addSubview:green];

    

    stump = [[UIView alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];

    stump.backgroundColor = [UIColor brownColor];

    // wood brown rgb 102, 51, 0

    stump.layer.borderColor = [UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:0 alpha:1.0].CGColor;

    stump.layer.borderWidth = 4;

    [self.view addSubview:stump];    

}

– (void)createFireWood

{    

    wood = [[UIView alloc] initWithFrame:CGRectMake(80, 170, 30, 80)];

    wood.backgroundColor = [UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:0 alpha:1.0];

    [self.view addSubview:wood];

}

– (void)createHatchet

{

    hatchet = [[UIView alloc] initWithFrame:CGRectMake(80, 140, 150, 20)];

    hatchet.backgroundColor = [UIColor brownColor];

    [self.view addSubview:hatchet];

    

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

    bladeA.backgroundColor = [UIColor darkGrayColor];

    [hatchet addSubview:bladeA];

    UIView *bladeB = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 50, 10)];

    bladeB.backgroundColor = [UIColor grayColor];

    [hatchet addSubview:bladeB];

}

– (void)createUI

{

    btn = [[UILabel alloc] initWithFrame:CGRectMake(220, 400, 100, 50)];

    btn.text = @”push”;

    btn.font = [UIFont fontWithName:@”MarkerFelt-Wide” size:40];

    btn.textColor = [UIColor whiteColor];

    btn.textAlignment = 1;

    btn.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];

    btn.layer.cornerRadius = 5.0;

    [self.view addSubview:btn];

    

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

        UIView *meter = [[UIView alloc] initWithFrame:CGRectMake(i * 20 + 10, 10 * (10-i) + 350, 10, 10 * i)];

        meter.backgroundColor = [UIColor whiteColor];

        meter.tag = i + 1;

        [self.view addSubview:meter];

    }

}

– (void)startTimer

{

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

}

– (void)tick:(NSTimer *)sender {

    if (powerlevel > 0) {

        powerlevel = powerlevel % 10 + 1;

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

            if (meter.tag > 0) {

                if(meter.tag <= powerlevel)

                {

                    meter.backgroundColor = [UIColor orangeColor];

                }

                else

                {

                    meter.backgroundColor = [UIColor whiteColor];

                }

            }

        }

    }

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    if (CGRectContainsPoint(btn.frame, p)) {

        powerlevel = 1;

    }

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    float duration = 1.0 / (float)powerlevel;

    

    [UIView animateWithDuration:1.0 animations:^{

        hatchet.transform = [self hachetangle:M_PI * (float)powerlevel * 0.05];

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:duration animations:^{

            hatchet.transform = [self hachetangle: –M_PI * 0.02];

        } completion:^(BOOL finished) {

            [self chopWood];

            [self performSelector:@selector(restart) withObject:nil afterDelay:1.0];

        }];

    }];

    powerlevel = 0;

}

– (CGAffineTransform)hachetangle:(float)angle

{

    CGAffineTransform t = CGAffineTransformMakeTranslation(100, 10);

    t = CGAffineTransformRotate(t, angle);

    t = CGAffineTransformTranslate(t, –100, –10);

    return t;

}

– (void)chopWood

{

    UIView *wood2 = [[UIView alloc] initWithFrame:wood.frame];

    wood2.backgroundColor = wood.backgroundColor;

    [self.view addSubview:wood2];

    

    wood.bounds = CGRectMake(0, 0, 20, 80);

    [UIView animateWithDuration:0.5 animations:^{

        wood.center = CGPointMake(400, 50);

        wood2.center = CGPointMake(-200, 50);

    } completion:^(BOOL finished) {

        [wood2 removeFromSuperview];

        [wood removeFromSuperview];

    }];

}

– (void)restart

{

    [self createFireWood];

    wood.transform = CGAffineTransformMakeTranslation(0, –200);

    [UIView animateWithDuration:0.5 animations:^{

        wood.transform = CGAffineTransformIdentity;

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end