飲みたいジュースのボタンをおして、
ジュースを買って遊ぼう!
という感じで、おままごとiPhoneゲームのサンプルコードを書いてみる
ポイント
ジュースとボタンはviewのタグで紐づけてみました、
ジュース.tag + 90 = ボタン.tag という関係。
ボタンは、タイマーで順番に光らせています。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface ViewController () {
int currentNumber;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createMachine];
[self startLighting];
}
– (void)createMachine
{
UIView *machine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
machine.backgroundColor = [UIColor redColor];
machine.alpha = 0.9;
[self.view addSubview:machine];
UIView *showcase = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];
showcase.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:showcase];
for (int i=0; i<3; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, i*100 + 90, 280, 30)];
line.backgroundColor = [UIColor blackColor];
[self.view addSubview:line];
for (int j=0; j<6; j++) {
float x = 40 + j*45;
float y = i*100 + 95;
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(x, y, 20, 20)];
btn.backgroundColor = [UIColor whiteColor];
btn.tag = i*6 + j + 10;
[self.view addSubview:btn];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[btn addGestureRecognizer:tap];
UIView *cannedDrink = [[UIView alloc] initWithFrame:CGRectMake(x – 5, y – 55, 30, 48)];
cannedDrink.backgroundColor = [self getColor:arc4random() % 5];
cannedDrink.layer.cornerRadius = 5;
cannedDrink.tag = i*6 + j + 100;
[self.view addSubview:cannedDrink];
}
}
UIView *exit = [[UIView alloc] initWithFrame:CGRectMake(20, 350, 280, 100)];
exit.backgroundColor = [UIColor blackColor];
[self.view addSubview:exit];
}
– (void)startLighting
{
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(lighting) userInfo:nil repeats:YES];
}
– (void)lighting
{
UIView *target = [self.view viewWithTag:currentNumber + 10];
UIView *light = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];
light.backgroundColor = [UIColor redColor];
light.center = target.center;
light.userInteractionEnabled = NO;
[self.view addSubview:light];
[UIView animateWithDuration:1.0 animations:^{
light.alpha = 0;
} completion:^(BOOL finished) {
[light removeFromSuperview];
}];
currentNumber = (currentNumber + 1) % 18;
}
– (void)tap:(UITapGestureRecognizer*)gr
{
UIView *cannedDrink = [self.view viewWithTag:gr.view.tag + 90];
UIView *purchased = [[UIView alloc] initWithFrame:cannedDrink.bounds];
purchased.backgroundColor = cannedDrink.backgroundColor;
purchased.layer.cornerRadius = cannedDrink.layer.cornerRadius;
purchased.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
purchased.center = CGPointMake(160, 380);
purchased.alpha = 0.6;
[self.view addSubview:purchased];
[UIView animateWithDuration:0.6 animations:^{
purchased.alpha = 1.0;
purchased.center = CGPointMake(160, 550);
} completion:^(BOOL finished) {
[purchased removeFromSuperview];
}];
}
– (UIColor*)getColor:(int)i
{
UIColor *c;
switch (i) {
case 0: c = UIColorHex(0xF95320); break;
case 1: c = UIColorHex(0xCC9620); break;
case 2: c = UIColorHex(0xE272D5); break;
case 3: c = UIColorHex(0x4229E2); break;
case 4: c = UIColorHex(0x70D1EF); break;
default:
break;
}
return c;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end