パンにレタスにハンバーグを上から落としてハンバーガーを作るサンプル
(XcodeのiOS6 iPhone Simulatorを使って動かしています。)
概要
バンズ、トマト、パティ、レタス、チーズのボタンを用意して、
押された材料を上から落としていくことで、
ハンバーガーを作るゲーム。
画面下にNext ボタンをつけて出来たら左に流す。
ポイント
UIBezierカーブを使って、バンズ等にそれっぽいカーブを付けていく
材料が停止する位置は、チーズのみ5ptとして、のこりは10pt
を積み上げていく。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface Hamburger : UIView
@property (nonatomic, strong) NSString *type;
@end
@implementation Hamburger
@synthesize type;
– (void)drawRect:(CGRect)rect
{
if ([type isEqual:@”top”]) {
[self createTop];
}
else if ([type isEqual:@”bottom”]) {
[self createBottom];
}
else if ([type isEqual:@”tomato”]) {
[self createTomato];
}
else if ([type isEqual:@”patty”]) {
[self createPatty];
}
else if ([type isEqual:@”lettuce”]) {
[self createLettuce];
}
else if ([type isEqual:@”cheese”]) {
[self createCheese];
}
}
– (void)createTop {
// 250,193,88
[[UIColor colorWithRed:250.0/255.0 green:193.0/255.0 blue:88.0/255.0 alpha:1.0] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, size.height*0.9);
CGContextAddCurveToPoint(ctx, 0, 0, size.width, 0, size.width, size.height*0.9);
CGContextMoveToPoint(ctx, size.width, size.height*0.9);
CGContextAddCurveToPoint(ctx, size.width, size.height, 0, size.height, 0, size.height*0.9);
CGContextFillPath(ctx);
}
– (void)createBottom {
// 250,193,88
[[UIColor colorWithRed:250.0/255.0 green:193.0/255.0 blue:88.0/255.0 alpha:1.0] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, size.height*0.2);
CGContextAddCurveToPoint(ctx, 0, 0, size.width, 0, size.width, size.height*0.2);
CGContextAddLineToPoint(ctx, size.width, size.height*0.6);
CGContextAddCurveToPoint(ctx, size.width, size.height, 0, size.height, 0, size.height*0.6);
CGContextFillPath(ctx);
}
– (void)createTomato {
[[UIColor colorWithRed:250.0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, size.height*0.2);
CGContextAddCurveToPoint(ctx, 0, 0, size.width, 0, size.width, size.height*0.2);
CGContextAddLineToPoint(ctx, size.width, size.height*0.6);
CGContextAddCurveToPoint(ctx, size.width, size.height, 0, size.height, 0, size.height*0.6);
CGContextFillPath(ctx);
}
– (void)createPatty {
// 156,49,0
[[UIColor colorWithRed:156.0/255.0 green:49/255.0 blue:0/255.0 alpha:1.0] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, size.height*0.4);
CGContextAddCurveToPoint(ctx, 0, 0, size.width, 0, size.width, size.height*0.4);
CGContextAddLineToPoint(ctx, size.width, size.height*0.6);
CGContextAddCurveToPoint(ctx, size.width, size.height, 0, size.height, 0, size.height*0.6);
CGContextFillPath(ctx);
}
– (void)createLettuce {
[[UIColor greenColor] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, 0);
for (int i=0; i<5; i++) {
float x0 = i * size.width * 0.2;
float x1 = (i + 1) * size.width * 0.2;
CGContextAddCurveToPoint(ctx, x0, size.height, x1, size.height, x1, 0);
}
CGContextAddLineToPoint(ctx, 0, 0);
CGContextFillPath(ctx);
}
– (void)createCheese {
[[UIColor yellowColor] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGContextMoveToPoint(ctx, 0, size.height*0.5);
CGContextAddLineToPoint(ctx, size.width*0.4, size.height*0.8);
CGContextAddQuadCurveToPoint(ctx, size.width*0.5, size.height, size.width*0.6, size.height*0.8);
CGContextAddLineToPoint(ctx, size.width, size.height*0.5);
CGContextAddLineToPoint(ctx, 0, 0);
CGContextFillPath(ctx);
}
@end
@interface ViewController (){
NSTimer *timer;
int floor;
int status;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
for (int i=0; i<500; i++) {
int x = (i / 50) * 40;
int y = (i % 50) * 20;
UIView *tile = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
tile.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.8 alpha:1.0];
tile.center = CGPointMake(x, y);
[self.view addSubview:tile];
}
[self createUI];
[self createLane];
[self start];
}
– (void)createUI
{
UIView *topBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 50, 40, 40)];
topBtn.layer.cornerRadius = 5.0;
topBtn.layer.borderWidth = 3.0;
topBtn.backgroundColor = [UIColor whiteColor];
Hamburger *top = [[Hamburger alloc] initWithFrame:CGRectMake(3, 10, 34, 20)];
top.backgroundColor = [UIColor clearColor];
top.type = @”top”;
[topBtn addSubview:top];
[self.view addSubview:topBtn];
UITapGestureRecognizer *tapA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createTopBun)];
[topBtn addGestureRecognizer:tapA];
UIView *bottomBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 100, 40, 40)];
bottomBtn.layer.cornerRadius = 5.0;
bottomBtn.layer.borderWidth = 3.0;
bottomBtn.backgroundColor = [UIColor whiteColor];
Hamburger *bottom = [[Hamburger alloc] initWithFrame:CGRectMake(3, 20, 34, 10)];
bottom.backgroundColor = [UIColor clearColor];
bottom.type = @”bottom”;
[bottomBtn addSubview:bottom];
[self.view addSubview:bottomBtn];
UITapGestureRecognizer *tapB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createBottomBun)];
[bottomBtn addGestureRecognizer:tapB];
UIView *tomatoBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 150, 40, 40)];
tomatoBtn.layer.cornerRadius = 5.0;
tomatoBtn.layer.borderWidth = 3.0;
tomatoBtn.backgroundColor = [UIColor whiteColor];
Hamburger *tomato = [[Hamburger alloc] initWithFrame:CGRectMake(3, 15, 34, 10)];
tomato.backgroundColor = [UIColor clearColor];
tomato.type = @”tomato”;
[tomatoBtn addSubview:tomato];
[self.view addSubview:tomatoBtn];
UITapGestureRecognizer *tapC = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createTomato)];
[tomatoBtn addGestureRecognizer:tapC];
UIView *pattyBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 200, 40, 40)];
pattyBtn.layer.cornerRadius = 5.0;
pattyBtn.layer.borderWidth = 3.0;
pattyBtn.backgroundColor = [UIColor whiteColor];
Hamburger *patty = [[Hamburger alloc] initWithFrame:CGRectMake(3, 15, 34, 10)];
patty.backgroundColor = [UIColor clearColor];
patty.type = @”patty”;
[pattyBtn addSubview:patty];
[self.view addSubview:pattyBtn];
UITapGestureRecognizer *tapD = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createPatty)];
[pattyBtn addGestureRecognizer:tapD];
UIView *lettuceBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 250, 40, 40)];
lettuceBtn.layer.cornerRadius = 5.0;
lettuceBtn.layer.borderWidth = 3.0;
lettuceBtn.backgroundColor = [UIColor whiteColor];
Hamburger *lettuce = [[Hamburger alloc] initWithFrame:CGRectMake(3, 15, 34, 10)];
lettuce.backgroundColor = [UIColor clearColor];
lettuce.type = @”lettuce”;
[lettuceBtn addSubview:lettuce];
[self.view addSubview:lettuceBtn];
UITapGestureRecognizer *tapE = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createLettuce)];
[lettuceBtn addGestureRecognizer:tapE];
UIView *cheeseBtn = [[UIView alloc] initWithFrame:CGRectMake(270, 300, 40, 40)];
cheeseBtn.layer.cornerRadius = 5.0;
cheeseBtn.layer.borderWidth = 3.0;
cheeseBtn.backgroundColor = [UIColor whiteColor];
Hamburger *cheese = [[Hamburger alloc] initWithFrame:CGRectMake(3, 15, 34, 10)];
cheese.backgroundColor = [UIColor clearColor];
cheese.type = @”cheese”;
[cheeseBtn addSubview:cheese];
[self.view addSubview:cheeseBtn];
UITapGestureRecognizer *tapF = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createCheese)];
[cheeseBtn addGestureRecognizer:tapF];
UILabel *next = [[UILabel alloc] init];
next.text = @”next”;
next.textColor = [UIColor grayColor];
next.font = [UIFont fontWithName:@”Chalkduster” size:30];
[next sizeToFit];
next.center = CGPointMake(160, 430);
next.layer.cornerRadius = 5.0;
next.userInteractionEnabled = YES;
[self.view addSubview:next];
UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(next)];
[next addGestureRecognizer:tapG];
}
– (void)createTopBun
{
Hamburger *burg = [[Hamburger alloc] initWithFrame:CGRectMake(50, 50, 60, 40)];
burg.backgroundColor = [UIColor clearColor];
burg.tag = 1;
burg.type = @”top”;
[self.view addSubview:burg];
}
– (void)createBottomBun
{
Hamburger *burg2 = [[Hamburger alloc] initWithFrame:CGRectMake(50, 150, 60, 20)];
burg2.backgroundColor = [UIColor clearColor];
burg2.tag = 1;
burg2.type = @”bottom”;
[self.view addSubview:burg2];
}
– (void)createTomato
{
Hamburger *tomato = [[Hamburger alloc] initWithFrame:CGRectMake(50, 150, 60, 20)];
tomato.backgroundColor = [UIColor clearColor];
tomato.tag = 1;
tomato.type = @”tomato”;
[self.view addSubview:tomato];
}
– (void)createPatty
{
Hamburger *patty = [[Hamburger alloc] initWithFrame:CGRectMake(50, 150, 60, 20)];
patty.backgroundColor = [UIColor clearColor];
patty.tag = 1;
patty.type = @”patty”;
[self.view addSubview:patty];
}
– (void)createLettuce
{
Hamburger *lettuce = [[Hamburger alloc] initWithFrame:CGRectMake(50, 150, 60, 20)];
lettuce.backgroundColor = [UIColor clearColor];
lettuce.tag = 1;
lettuce.type = @”lettuce”;
[self.view addSubview:lettuce];
}
– (void)createCheese
{
Hamburger *cheese = [[Hamburger alloc] initWithFrame:CGRectMake(50, 150, 60, 20)];
cheese.backgroundColor = [UIColor clearColor];
cheese.tag = 1;
cheese.type = @”cheese”;
[self.view addSubview:cheese];
}
– (void)createLane
{
for (int i=0; i<20; i++) {
float x = i * 20;
float y = 380;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, 20, 20)];
v.layer.cornerRadius = 10.0;
v.layer.borderWidth = 1.0;
v.layer.borderColor = [UIColor lightGrayColor].CGColor;
v.backgroundColor = [UIColor darkGrayColor];
v.tag = 2;
[self.view addSubview:v];
}
}
– (void)next
{
status = 1;
}
– (void)tick:(NSTimer*)sender
{
for (UIView *v in self.view.subviews) {
if (v.tag == 1 && status == 0) {
v.center = CGPointMake(v.center.x, v.center.y + 1);
float adjust = [((Hamburger*)v).type isEqual:@”cheese”] || [((Hamburger*)v).type isEqual:@”lettuce”] ? 5 : 10;
if ((v.frame.origin.y + v.frame.size.height) >= (400 – floor – adjust)) {
v.tag = 3;
floor += adjust;
}
}
if (v.tag >= 1 && status > 0) {
v.center = CGPointMake(v.center.x – 2, v.center.y);
if (v.tag == 2 && v.center.x < – 10) {
v.center = CGPointMake(20 * v.bounds.size.width, v.center.y);
status++;
if (status > 20) {
status = 0;
floor = 0;
for (UIView *v in self.view.subviews) {
if (v.tag == 3) {
[v removeFromSuperview];
}
}
}
}
}
}
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end