数字の下に、丸を10個表示しておいて、数字の個数だけ丸をタッチすると次の数がでる
(XcodeのiOS6 iPhone Simulatorで試しています。)
ポイント
・バスが数字を当てるたびに前に進んでいくようにする
・丸にUITapGestureを加え、対応するメソッド内で正否判定を実施
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *body;
UIView *numberPanel;
UILabel *nLabel;
NSTimer *timer;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createCar];
[self start];
}
– (void)createCar
{
body = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
body.backgroundColor = [UIColor redColor];
[self.view addSubview:body];
UIView *frontWindow = [[UIView alloc] initWithFrame:CGRectMake(65, 5, 10, 20)];
frontWindow.backgroundColor = [UIColor blueColor];
[body addSubview:frontWindow];
UILabel *fWheel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
fWheel.center= CGPointMake(60, 40);
fWheel.backgroundColor = [UIColor lightGrayColor];
fWheel.layer.borderWidth = 5.0;
fWheel.layer.borderColor = [UIColor blackColor].CGColor;
fWheel.layer.cornerRadius = 10;
[body addSubview:fWheel];
fWheel.tag = 1;
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, 18)];
line.center = CGPointMake(10,10);
line.backgroundColor = [UIColor blackColor];
[fWheel addSubview:line];
UIView *rWheel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
rWheel.center= CGPointMake(10, 40);
rWheel.backgroundColor = [UIColor lightGrayColor];
rWheel.layer.borderWidth = 5.0;
rWheel.layer.borderColor = [UIColor blackColor].CGColor;
rWheel.layer.cornerRadius = 10;
[body addSubview:rWheel];
rWheel.tag = 1;
line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, 18)];
line.center = CGPointMake(10,10);
line.backgroundColor = [UIColor blackColor];
[rWheel addSubview:line];
[UIView animateWithDuration:1.0 animations:^{
body.center = CGPointMake(60, 300);
}];
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
static int counter;
if (body.tag == 0) {
if (counter % 100 == 50) {
[self createNumPanel:counter/100];
body.tag = 1;
}
counter++;
for (UIView *v in body.subviews) {
if (v.tag == 1) {
v.transform = CGAffineTransformRotate(v.transform, M_PI*0.1);
}
}
}
}
– (void)createNumPanel:(int)number
{
numberPanel = [[UIView alloc] initWithFrame:CGRectMake(120, –300, 200, 300)];
numberPanel.backgroundColor = [UIColor blueColor];
[self.view addSubview:numberPanel];
nLabel = [[UILabel alloc] init];
nLabel.text = [NSString stringWithFormat:@”%d”, number+1];
nLabel.font = [UIFont boldSystemFontOfSize:80];
nLabel.textColor = [UIColor whiteColor];
nLabel.backgroundColor = [UIColor clearColor];
[nLabel sizeToFit];
nLabel.center = CGPointMake(100, 100);
[numberPanel addSubview:nLabel];
for (int i=0; i<10; i++) {
float x = (i % 5) * 40 + 10;
float y = (i / 5) * 40 + 200;
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(x, y, 20, 20)];
btn.backgroundColor = [UIColor whiteColor];
btn.layer.cornerRadius = 10.0;
[numberPanel addSubview:btn];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[btn addGestureRecognizer:tap];
}
[UIView animateWithDuration:0.5 animations:^{
numberPanel.center = CGPointMake(numberPanel.center.x, 200);
}];
}
– (void)tap:(UIGestureRecognizer*)gr
{
gr.view.backgroundColor = [UIColor yellowColor];
if (body.tag == [nLabel.text intValue]) {
[UIView animateWithDuration:1.5 animations:^{
numberPanel.center = CGPointMake(numberPanel.center.x, –300);
} completion:^(BOOL finished) {
numberPanel = nil;
body.tag = 0;
}];
}
body.tag++;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end