緑、黄色、オレンジ、紫、青の5つの封筒に、同じ色の紙をいれていこう!というシンプルなゲームをiPhoneアプリで作ってみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *frontView;
UIView *mail;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
frontView = [[UIView alloc] initWithFrame:self.view.bounds];
frontView.backgroundColor = [UIColor clearColor];
frontView.userInteractionEnabled = NO;
[self createEnvelopes];
[self.view addSubview:frontView];
mail = [self createLeaf];
mail.center = CGPointMake(40, 50);
mail.tag = 1;
[self addGesture];
[self startTimer];
}
– (void)createEnvelopes
{
for (int i=0; i<5; i++) {
CGPoint p = CGPointMake(60 * i + 40, 450);
[self createEnvelope:[self color:i] point:p];
}
}
– (void)createEnvelope:(UIColor*)color point:(CGPoint)p
{
float w = 50;
float h = w/1.618;
UIView *front = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
front.center = p;
[frontView addSubview:front];
UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
back.center = p;
back.userInteractionEnabled = NO;
[self.view addSubview:back];
UIBezierPath *pathTriangleA = [UIBezierPath bezierPath];
[pathTriangleA moveToPoint:CGPointMake(0, 0)];
[pathTriangleA addLineToPoint:CGPointMake(w, 0)];
[pathTriangleA addLineToPoint:CGPointMake(w/2.0, h/2.0)];
[pathTriangleA closePath];
CAShapeLayer *partA = [CAShapeLayer layer];
partA.fillColor = color.CGColor;
partA.path = pathTriangleA.CGPath;
[back.layer addSublayer:partA];
CAShapeLayer *partB = [[CAShapeLayer alloc] init];
partB.frame = CGRectMake(0, 0, w, h);
partB.anchorPoint = CGPointMake(0.5, 0.5);
partB.position = CGPointMake(w/2.0, h/2.0);
partB.fillColor = color.CGColor;
partB.path = pathTriangleA.CGPath;
partB.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
[front.layer addSublayer:partB];
CAShapeLayer *partC = [[CAShapeLayer alloc] init];
partC.frame = CGRectMake(0, 0, w, h);
partC.anchorPoint = CGPointMake(0.5, 0);
partC.position = CGPointMake(w/2.0, 0);
partC.fillColor = color.CGColor;
partC.path = pathTriangleA.CGPath;
partC.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
[back.layer addSublayer:partC];
UIBezierPath *pathTriangleB = [UIBezierPath bezierPath];
[pathTriangleB moveToPoint:CGPointMake(0, 0)];
[pathTriangleB addLineToPoint:CGPointMake(0, h)];
[pathTriangleB addLineToPoint:CGPointMake(w/2.0, h/2.0)];
[pathTriangleB closePath];
CAShapeLayer *partD = [[CAShapeLayer alloc] init];
partD.fillColor = CGColorCreateCopyWithAlpha(color.CGColor, 0.6);
partD.path = pathTriangleB.CGPath;
[front.layer addSublayer:partD];
// add white
CAShapeLayer *partE = [[CAShapeLayer alloc] init];
partE.fillColor = [UIColor colorWithWhite:1 alpha:0.3].CGColor;
partE.path = pathTriangleB.CGPath;
[front.layer addSublayer:partE];
CAShapeLayer *partF = [[CAShapeLayer alloc] init];
partF.frame = CGRectMake(0, 0, w, h);
partF.anchorPoint = CGPointMake(0.5, 0.5);
partF.position = CGPointMake(w/2.0, h/2.0);
partF.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
partF.fillColor = CGColorCreateCopyWithAlpha(color.CGColor, 0.6);
partF.path = pathTriangleB.CGPath;
[front.layer addSublayer:partF];
// add white
CAShapeLayer *partG = [[CAShapeLayer alloc] init];
partG.frame = CGRectMake(0, 0, w, h);
partG.anchorPoint = CGPointMake(0.5, 0.5);
partG.position = CGPointMake(w/2.0, h/2.0);
partG.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
partG.fillColor = [UIColor colorWithWhite:1 alpha:0.3].CGColor;
partG.path = pathTriangleB.CGPath;
[front.layer addSublayer:partG];
}
– (UIView *)createLeaf
{
float w = 40;
float h = w * sqrt(2);
UIView *leaf = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
leaf.backgroundColor = [UIColor clearColor];
[self.view insertSubview:leaf belowSubview:frontView];
UIBezierPath *whitePath = [UIBezierPath bezierPath];
[whitePath moveToPoint:CGPointMake(0, 0)];
[whitePath addLineToPoint:CGPointMake(w * 0.8, 0)];
[whitePath addLineToPoint:CGPointMake(w, w * 0.2)];
[whitePath addLineToPoint:CGPointMake(w, h)];
[whitePath addLineToPoint:CGPointMake(0, h)];
[whitePath closePath];
CAShapeLayer *wSl = [CAShapeLayer layer];
wSl.fillColor = [UIColor whiteColor].CGColor;
wSl.strokeColor = [UIColor lightGrayColor].CGColor;
wSl.lineWidth = 1;
wSl.path = whitePath.CGPath;
[leaf.layer addSublayer:wSl];
UIBezierPath *colorPath = [UIBezierPath bezierPath];
[colorPath moveToPoint:CGPointMake(w * 0.8, 0)];
[colorPath addLineToPoint:CGPointMake(w * 0.8, w * 0.2)];
[colorPath addLineToPoint:CGPointMake(w, w * 0.2)];
[colorPath closePath];
[colorPath addArcWithCenter:CGPointMake(w/2.0, h/4.0) radius:w/5.0 startAngle:0 endAngle:2 * M_PI clockwise:YES];
int colorNo = arc4random() % 5;
CAShapeLayer *colorSl = [CAShapeLayer layer];
colorSl.fillColor = [self color:colorNo].CGColor;
colorSl.path = colorPath.CGPath;
[leaf.layer addSublayer:colorSl];
leaf.userInteractionEnabled = NO;
return leaf;
}
– (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
mail.center = CGPointMake(mail.center.x, mail.center.y + 5);
if (mail.center.y > 435) {
mail.center = CGPointMake(mail.center.x, 435);
mail = nil;
mail = [self createLeaf];
int n = arc4random() % 5;
float p = 40 + 60 * n;
mail.center = CGPointMake(p, 0);
mail.tag = n + 1;
}
}
– (void)addGesture
{
UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(right)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:right];
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(left)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:left];
}
– (void)right
{
if (mail.tag < 5) {
mail.tag = mail.tag + 1;
[UIView animateWithDuration:0.1 animations:^{
mail.center = CGPointMake(mail.center.x + 60, mail.center.y);
}];
}
}
– (void)left
{
if (mail.tag > 1) {
mail.tag = mail.tag – 1;
[UIView animateWithDuration:0.3 animations:^{
mail.center = CGPointMake(mail.center.x – 60, mail.center.y);
}];
}
}
#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]
– (UIColor*)color:(int)i
{
switch (i) {
case 0:
return UIColorHex(0x75FF89);
case 1:
return UIColorHex(0xE8D96A);
case 2:
return UIColorHex(0xFFAE81);
case 3:
return UIColorHex(0xFC75FF);
case 4:
return UIColorHex(0x698FFF);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end