「のばして、のせて、包む」
3つの動作で餃子を作るiPhoneゲームのサンプルです。
工場のボタンを指で引っ張りましょう。
昨日晩が餃子だったので、作ってみようと思い立ちました。
環境
XcodeのiOS6 iPhone Simulatorで動かしています。
ポイント
1. 今回から、カラーテーマを使っていこうと思い、kulerというadobeのカラーパレットを利用。
※この通り、自分、未熟すぎてまだ効果が現れるには時間が。
2.工場のレーン的な部分は、NSTimerを利用して、餃子のUIViewのセンターを – 1していくことで動かす。
3.CAShapeLayerでUIViewを中空にした。プロパティ fillRuleでkCAFillRuleEvenOddを利用
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSTimer *timer;
NSMutableArray *gyoza;
NSMutableArray *switches;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self colorPallet:5];
[self createLane];
[self createUI];
[self start];
}
– (void)createLane
{
UIView *base = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 100)];
base.backgroundColor = [self colorPallet:2];
[self.view addSubview:base];
UIView *lane = [[UIView alloc] initWithFrame:CGRectMake(0, 350, 320, 50)];
lane.backgroundColor = [self colorPallet:3];
[self.view addSubview:lane];
}
– (void)createUI
{
switches = [[NSMutableArray alloc] init];
for (int i=0; i<3; i++) {
UIView *inA = [[UIView alloc] initWithFrame:CGRectMake(55 + 80*i, 150, 30, 120)];
inA.backgroundColor = [self colorPallet:4];
[self.view addSubview:inA];
UIView *boxA = [[UIView alloc] initWithFrame:CGRectMake(50 + 80*i, 155, 40, 100)];
boxA.backgroundColor = [UIColor clearColor];
boxA.userInteractionEnabled = NO;
[self.view addSubview:boxA];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:boxA.bounds cornerRadius:10];
CGRect innerRect = CGRectMake(10, 20, 10, 60);
[path appendPath:[UIBezierPath bezierPathWithRoundedRect:innerRect cornerRadius:5]];
CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:boxA.layer];
sl.fillRule = kCAFillRuleEvenOdd;
sl.fillColor = [self colorPallet:1].CGColor;
sl.path = path.CGPath;
[boxA.layer addSublayer:sl];
[switches addObject:inA];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[inA addGestureRecognizer:pan];
}
}
– (void)pan:(UIPanGestureRecognizer*)gr
{
CGPoint velocity = [gr velocityInView:self.view];
if (velocity.y > 10 && gr.view.tag == 0) {
gr.view.tag = 1;
[UIView animateWithDuration:0.5 animations:^{
gr.view.center = CGPointMake(gr.view.center.x, gr.view.center.y + 80);
} completion:^(BOOL finished) {
gr.view.tag = 0;
[UIView animateWithDuration:0.2 animations:^{
gr.view.center = CGPointMake(gr.view.center.x, gr.view.center.y – 80);
}];
}];
}
}
– (UIColor*)colorPallet:(int)i
{
UIColor *color;
switch (i) {
case 1: // 46 9 29
color = [UIColor colorWithRed:46.0/255.0 green:9.0/255.0 blue:29.0/255.0 alpha:1.0];
break;
case 2: // 217 105 0
color = [UIColor colorWithRed:217.0/255.0 green:105.0/255.0 blue:0/255.0 alpha:1.0];
break;
case 3: // 255 187 0
color = [UIColor colorWithRed:255.0/255.0 green:187.0/255.0 blue:0/255.0 alpha:1.0];
break;
case 4: // 231 255 0
color = [UIColor colorWithRed:231.0/255.0 green:255.0/255.0 blue:0/255.0 alpha:1.0];
break;
case 5: // 4 117 100
color = [UIColor colorWithRed:4.0/255.0 green:117.0/255.0 blue:100.0/255.0 alpha:1.0];
break;
default:
break;
}
return color;
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
static float lastTime;
if (lastTime > 2) {
[self createTane];
lastTime = 0;
}
lastTime += sender.timeInterval;
for (UIView *v in gyoza) {
v.center = CGPointMake(v.center.x – 1, v.center.y);
for (int i=0; i<[switches count]; i++) {
UIView *s = [switches objectAtIndex:i];
CGRect r = [s.layer.presentationLayer frame];
if (CGRectIntersectsRect(v.frame, r)) {
v.tag = 3 – i;
[self changeMode:v];
}
}
}
}
– (void)changeMode:(UIView*)v
{
if (v.tag == 1) {
CATransform3D transform;
transform = CATransform3DMakeRotation(M_PI * 0.4, 1, 0, 0);
transform = CATransform3DScale(transform, 2.0, 2.0, 1.0);
v.layer.zPosition = 40;
v.layer.transform = transform;
}
else if (v.tag == 2)
{
UIView *meet = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 10, 10)];
meet.layer.cornerRadius = 5.0;
meet.backgroundColor = [UIColor brownColor];
[v addSubview:meet];
}
else if (v.tag == 3)
{
CGSize s = v.bounds.size;
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, s.height * 0.5)];
[path addCurveToPoint:CGPointMake(s.width, s.height * 0.5) controlPoint1:CGPointMake(s.width*0.1, 0) controlPoint2:CGPointMake(s.width*0.9, 0)];
[path addLineToPoint:CGPointMake(0, s.height * 0.5)];
CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:v.layer];
sl.fillColor = [UIColor colorWithWhite:1.0 alpha:0.9].CGColor;
sl.path = path.CGPath;
v.transform = CGAffineTransformMakeScale(1.5, 1.5);
v.center = CGPointMake(v.center.x, v.center.y + 1);
v.backgroundColor = [UIColor clearColor];
[v.layer addSublayer:sl];
for (int i=0; i<4; i++) {
float w = s.width * 0.2;
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.5, 5)];
line.backgroundColor = [UIColor blackColor];
line.center = CGPointMake(w * (i+1), 6);
[v addSubview:line];
}
}
}
– (void)createTane
{
if (!gyoza) {
gyoza = [[NSMutableArray alloc] init];
}
UIView *tane = [[UIView alloc] initWithFrame:CGRectMake(320, 340, 30, 30)];
tane.backgroundColor = [UIColor whiteColor];
tane.layer.cornerRadius = 15;
[self.view addSubview:tane];
[gyoza addObject:tane];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end