視力検査でよくみるCを使ったかんたんなiPhoneゲームを作ってみます。オレンジ色のCと同じ方向を向いたCを一つだけ用意して、それをタッチするという感じにします。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *sample;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:3];
[self createSample];
[self createC];
[self turn];
}
– (void)createSample
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(40, 40) radius:40 startAngle:M_PI*0.1 endAngle:-M_PI*0.1 clockwise:YES];
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [UIColor clearColor].CGColor;
sl.strokeColor = [self color:4].CGColor;
sl.lineWidth = 20;
sl.path = path.CGPath;
sample = [[UIView alloc] initWithFrame:CGRectMake(120, 40, 80, 80)];
[self.view addSubview:sample];
[sample.layer addSublayer:sl];
}
– (void)createC
{
for (int i=1; i<12; i++) {
float angle = i * M_PI / 6.0;
float x = 120 * cos(angle) + 160;
float y = 120 * sin(angle) + 320;
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(20, 20) radius:20 startAngle:M_PI*0.1 endAngle:-M_PI*0.1 clockwise:YES];
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [UIColor clearColor].CGColor;
sl.strokeColor = [self color:1].CGColor;
sl.lineWidth = 10;
sl.path = path.CGPath;
UIView *c = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
c.center = CGPointMake(x, y);
[self.view addSubview:c];
[c.layer addSublayer:sl];
c.tag = i;
}
}
– (void)turn
{
float sampleAngle = (arc4random() % 4) * M_PI * 0.5;
for (int i=1; i<12; i++) {
UIView *c = [self.view viewWithTag:i];
[UIView animateWithDuration:0.5 animations:^{
c.transform = CGAffineTransformMakeRotation(sampleAngle + M_PI);
}];
}
int n = arc4random() % 11 + 1;
UIView *target = [self.view viewWithTag:n];
[UIView animateWithDuration:0.5 animations:^{
sample.transform = CGAffineTransformMakeRotation(sampleAngle);
target.transform = CGAffineTransformMakeRotation(sampleAngle);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[target addGestureRecognizer:tap];
}
– (void)tap:(UITapGestureRecognizer*)gr
{
// clear
UILabel *l = [[UILabel alloc] init];
l.text = @”Clear”;
l.font = [UIFont boldSystemFontOfSize:40];
l.textColor = [self color:0];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
l.center = CGPointMake(160, 284);
[self.view addSubview:l];
CGAffineTransform t = CGAffineTransformMakeTranslation(400, 0);
l.transform = CGAffineTransformRotate(t, M_PI);
[UIView animateWithDuration:0.3 animations:^{
l.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[l performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.8];
}];
//restart
[self performSelector:@selector(restart) withObject:nil afterDelay:1.0];
}
– (void)restart
{
[self turn];
}
#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(0xFFFFFA);
case 1:
return UIColorHex(0xA1A194);
case 2:
return UIColorHex(0x58605F);
case 3:
return UIColorHex(0x464646);
case 4:
return UIColorHex(0xFF6600);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end