ボタンを連打して、ロケットを飛ばしてみる。
(XcodeのiOS6 iPhone Simulatorで試しています。)
こんな感じ
・3段で分離させる
・ボタンを押すとゲージが上がるように
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
CADisplayLink *timer;
UIView *part1, *part2, *part3;
int status;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createRocket];
[self createConsole];
}
– (void)createConsole
{
UILabel *push = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
push.center = CGPointMake(280, 400);
push.text = @”push”;
push.font = [UIFont boldSystemFontOfSize:20];
push.textColor = [UIColor whiteColor];
push.textAlignment = 1;
push.backgroundColor = [UIColor redColor];
push.layer.cornerRadius = 30.0;
push.layer.borderColor = [UIColor blackColor].CGColor;
push.layer.borderWidth = 2.0;
[self.view addSubview:push];
push.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[push addGestureRecognizer:tap];
}
– (void)tap:(UIGestureRecognizer*)gr;
{
if (status == 0) {
static int counter;
counter++;
float x = gr.view.center.x;
float y = gr.view.center.y – 50 – counter * 12;
UIView *meter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 10)];
meter.center = CGPointMake(x, y);
meter.backgroundColor = [UIColor redColor];
meter.layer.cornerRadius = 5.0;
[self.view addSubview:meter];
if (counter > 25) {
status = 1;
[self start];
}
}
}
– (void)start
{
timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateDisp:)];
[timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
– (void)updateDisp:(CADisplayLink*)sender
{
// ロケットを動かす
if (status == 1) {
part1.center = CGPointMake(part1.center.x, part1.center.y – 1);
part2.center = CGPointMake(part2.center.x, part2.center.y – 1);
part3.center = CGPointMake(part3.center.x, part3.center.y – 1);
[self fire:CGPointMake(part1.center.x, part1.center.y + part1.frame.size.height * 0.5)];
} else if (status == 2) {
part1.center = CGPointMake(part1.center.x – 1, part1.center.y + 1);
part2.center = CGPointMake(part2.center.x, part2.center.y – 1);
part3.center = CGPointMake(part3.center.x, part3.center.y – 1);
[self fire:CGPointMake(part2.center.x, part2.center.y + part2.frame.size.height * 0.5)];
} else if (status == 3) {
part1.center = CGPointMake(part1.center.x – 1, part1.center.y + 1);
part2.center = CGPointMake(part2.center.x + 1, part2.center.y + 1);
part3.center = CGPointMake(part3.center.x, part3.center.y – 1);
[self fire:CGPointMake(part3.center.x, part3.center.y + part3.frame.size.height * 0.5)];
}
// 先頭の高さで分離
if (status == 1 && part3.center.y < 200) {
status = 2;
} else if (status == 2 && part3.center.y < 100) {
status = 3;
}
}
– (void)fire:(CGPoint)p
{
float x = p.x + arc4random() % 10 – 5.0;
float y = p.y;
UIView *fire = [[UIView alloc] initWithFrame:CGRectMake(x, y, 5, 5)];
fire.backgroundColor = [UIColor redColor];
[self.view addSubview:fire];
float dx = arc4random() % 20 – 10.0;
[UIView animateWithDuration:0.2 animations:^{
fire.center = CGPointMake(fire.center.x + dx, fire.center.y + 10);
} completion:^(BOOL finished) {
[fire removeFromSuperview];
}];
}
– (void)createRocket
{
float x = 160.0;
float y = 420.0;
part1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
part1.center = CGPointMake(x, y);
part1.backgroundColor = [UIColor redColor];
[self.view addSubview:part1];
y -= part1.bounds.size.height;
part2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
part2.center = CGPointMake(x,y);
part2.backgroundColor = [UIColor yellowColor];
[self.view addSubview:part2];
y -= part2.bounds.size.height;
part3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
part3.center = CGPointMake(x, y);
part3.backgroundColor = [UIColor greenColor];
[self.view addSubview:part3];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end