光る順番を記憶してタッチしていくiPhoneアプリのサンプルコードを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *flashPattern;
@property (nonatomic, weak) UIButton *start;
@property int flashCount;
@property int touchCount;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
[self createLamp];
self.start = [self createStartButton];
}
– (UIButton *)createStartButton
{
UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[startBtn setTitle:@”start” forState:UIControlStateNormal];
startBtn.titleLabel.font = [UIFont systemFontOfSize:30];
[startBtn sizeToFit];
startBtn.center = CGPointMake(160, 430);
[self.view addSubview:startBtn];
[startBtn addTarget:self action:@selector(startFlash) forControlEvents:UIControlEventTouchUpInside];
return startBtn;
}
– (void)createLamp
{
for (int i=0; i<4; i++) {
float x = (i % 2) * 160 + 40;
float y = (i / 2) * 200 + 100;
UIView *lampView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 80, 80)];
lampView.tag = i + 1;
lampView.backgroundColor = [UIColor colorWithHue:0.1+0.2*i saturation:1 brightness:1 alpha:1];
lampView.layer.cornerRadius = 40;
[self.view addSubview:lampView];
}
}
– (void)startFlash
{
self.flashPattern = [NSMutableArray array];
for (int i=0; i<4; i++) {
[self.flashPattern addObject:@((arc4random() % 4) + 1)];
}
self.start.userInteractionEnabled = NO;
self.flashCount = 0;
self.touchCount = 0;
[self flash];
}
– (void)flash
{
if (self.flashPattern.count > self.flashCount) {
NSNumber *n = self.flashPattern[self.flashCount++];
UIView *lamp = [self.view viewWithTag:[n intValue]];
UIView *backLight = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
backLight.backgroundColor = [UIColor whiteColor];
backLight.layer.cornerRadius = 50;
backLight.center = lamp.center;
[self.view insertSubview:backLight aboveSubview:lamp];
[UIView animateWithDuration:0.5 animations:^{
backLight.alpha = 0;
} completion:^(BOOL finished) {
[backLight removeFromSuperview];
[self performSelector:@selector(flash) withObject:nil afterDelay:0.5];
}];
// update button label
CGPoint o = self.start.center;
NSString *dotIncrement = [@”” stringByPaddingToLength:3*self.flashCount withString: @”.” startingAtIndex:0];
[self.start setTitle:dotIncrement forState:UIControlStateNormal];
[self.start sizeToFit];
self.start.center = o;
} else {
// update button label
[self.start setTitle:@”Touch!” forState:UIControlStateNormal];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self.start.titleLabel.text isEqualToString:@”Touch!”]) {
CGPoint p = [[touches anyObject] locationInView:self.view];
[self.view.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
if (obj.tag > 0 && CGRectContainsPoint(obj.frame, p)) {
UIView *backLight = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
backLight.backgroundColor = [UIColor whiteColor];
backLight.layer.cornerRadius = 50;
backLight.center = obj.center;
[self.view insertSubview:backLight belowSubview:obj];
[UIView animateWithDuration:0.5 animations:^{
backLight.alpha = 0;
} completion:^(BOOL finished) {
[backLight removeFromSuperview];
}];
if ([self.flashPattern[self.touchCount] intValue] == obj.tag) {
// correct!
if (self.touchCount < self.flashPattern.count – 1) {
self.touchCount++;
} else {
[self.start setTitle:@”Clear!” forState:UIControlStateNormal];
[self performSelector:@selector(restart) withObject:nil afterDelay:2.0];
}
} else {
// miss
[self.start setTitle:@”miss!” forState:UIControlStateNormal];
[self performSelector:@selector(restart) withObject:nil afterDelay:2.0];
}
}
}];
}
}
– (void)restart
{
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
[self createLamp];
self.start = [self createStartButton];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end