四葉のクローバーをさがすiPhoneゲームアプリを作ってみます。画面の上の方にタイマーを表示して、四葉のクローバーを全部見つけ出すまでの時間を測定するようにしましょう。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
クローバーは、UIBezierPathのaddCurveToPointをつかって、ハート形の葉っぱに仕立てました。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UILabel *timerLabel;
NSTimer *timer;
int luckyCount;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:0];
// create clover
for (int i = 0; i<30; i++) {
UIView *c = [self createClover];
float x = (i % 5) * 60 + 30;
float y = (i / 5) * 60 + 150;
c.center = CGPointMake(x, y);
}
[self createTimer];
}
– (UIView *)createClover
{
UIView *clover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
clover.backgroundColor = [self color:0];
[self.view addSubview:clover];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
CGPoint o = CGPointMake(25, 25);
[path addLineToPoint:o];
// lucky clover or normal clover
luckyCount = 0;
int rand = arc4random() % 5;
float count = 3.0;
if (rand == 0) {
// lucky
luckyCount++;
count = 4.0;
clover.tag = 1;
}
float dAngle = 2.0 * M_PI / count;
for (int i = 0; i<count; i++) {
// leaf
float x = 15.0 * cos(dAngle * i) + o.x;
float y = 15.0 * sin(dAngle * i) + o.y;
CGPoint mid = CGPointMake(x, y);
x = 25.0 * cos(dAngle * i – M_PI / 4.0) + o.x;
y = 25.0 * sin(dAngle * i – M_PI / 4.0) + o.y;
CGPoint cp1 = CGPointMake(x, y);
x = 25.0 * cos(dAngle * i) + o.x;
y = 25.0 * sin(dAngle * i) + o.y;
CGPoint cp2 = CGPointMake(x, y);
[path addCurveToPoint:mid controlPoint1:cp1 controlPoint2:cp2];
// same last cp2
cp1 = CGPointMake(x, y);
x = 25.0 * cos(dAngle * i + M_PI / 4.0) + o.x;
y = 25.0 * sin(dAngle * i + M_PI / 4.0) + o.y;
cp2 = CGPointMake(x, y);
[path addCurveToPoint:o controlPoint1:cp1 controlPoint2:cp2];
}
CAShapeLayer *sl = [[CAShapeLayer alloc] init];
sl.strokeColor = [self color:1].CGColor;
sl.lineWidth = 2;
sl.fillColor = [self color:3].CGColor;
sl.path = path.CGPath;
[clover.layer addSublayer:sl];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectClover:)];
[clover addGestureRecognizer:tap];
return clover;
}
– (void)createTimer
{
timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
timerLabel.text = @”00.00″;
timerLabel.textColor = [self color:2];
timerLabel.textAlignment = NSTextAlignmentCenter;
timerLabel.font = [UIFont boldSystemFontOfSize:40];
timerLabel.backgroundColor = [self color:4];
timerLabel.layer.cornerRadius = 20;
timerLabel.layer.borderColor = [self color:1].CGColor;
timerLabel.layer.borderWidth = 2;
[self.view addSubview:timerLabel];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)selectClover:(UITapGestureRecognizer*)gr
{
if (gr.view.tag == 1) {
gr.view.tag = 2;
gr.view.userInteractionEnabled = NO;
[UIView animateWithDuration:0.3 animations:^{
gr.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
}];
} else {
[UIView animateWithDuration:0.3 animations:^{
gr.view.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
gr.view.transform = CGAffineTransformIdentity;
}];
}
// check clear
BOOL isClear = YES;
for (UIView *v in self.view.subviews) {
if (v.tag == 1) {
isClear = NO;
}
}
if (isClear) {
[timer invalidate];
UILabel *clearLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 220, 220, 50)];
clearLabel.text = @”Finish!”;
clearLabel.textAlignment = NSTextAlignmentCenter;
clearLabel.textColor = [self color:0];
clearLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:.7];
[self.view addSubview:clearLabel];
clearLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(restart)];
[clearLabel addGestureRecognizer:tap];
}
}
– (void)tick:(NSTimer*)sender
{
float currentTime = [timerLabel.text floatValue];
currentTime += sender.timeInterval;
timerLabel.text = [NSString stringWithFormat:@”%.2f”, currentTime];
}
– (void)restart
{
for (UIView *v in self.view.subviews) {
[v removeFromSuperview];
}
// create clover
for (int i = 0; i<30; i++) {
UIView *c = [self createClover];
float x = (i % 5) * 60 + 30;
float y = (i / 5) * 60 + 150;
c.center = CGPointMake(x, y);
}
[self createTimer];
}
#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(0xEEEBF2);
case 1:
return UIColorHex(0x328C59);
case 2:
return UIColorHex(0x49A660);
case 3:
return UIColorHex(0x94BF54);
case 4:
return UIColorHex(0xE5F2B3);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end