お父さんゆび(親指)の機敏性を測定!
という感じの子供向けiPhoneゲームのサンプルコードを書いてみた。
ポイント
デジタルなフォントは「DBLCDTempBlack」を利用しました。
白い線の間にボタンを仕込んでおき、親指でタッチすると
黒いボールが跳ねていくようにしています。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
#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]
@interface ViewController () {
UIView *jumper;
float time;
UILabel *timerLabel;
int count;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = UIColorHex(0x737373);
[self createLines];
[self createJumper];
[self createTimer];
}
– (void)createLines
{
for (int i=0; i<3; i++) {
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 100)];
btn.center = CGPointMake(80 + 80 * i, 300);
btn.backgroundColor = UIColorHex(0x737373);
[self.view addSubview:btn];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump:)];
[btn addGestureRecognizer:tap];
}
for (int i=0; i<2; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 100)];
line.center = CGPointMake(120 + 80*i, 300);
line.backgroundColor = UIColorHex(0xF2F2F2);
[self.view addSubview:line];
}
}
– (void)createJumper
{
jumper = [[UIView alloc] initWithFrame:CGRectMake(20, 300, 30, 30)];
jumper.layer.cornerRadius = 15;
jumper.backgroundColor = UIColorHex(0x0D0D0D);
[self.view addSubview:jumper];
}
– (void)jump:(UITapGestureRecognizer*)gr
{
if (time == 0) {
[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
count++;
[UIView animateWithDuration:0.1 animations:^{
jumper.transform = CGAffineTransformMakeTranslation(0, –50);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
jumper.transform = CGAffineTransformIdentity;
}];
}];
[UIView animateWithDuration:0.2 animations:^{
jumper.center = gr.view.center;
}];
}
– (void)createTimer
{
timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 220, 110)];
timerLabel.font = [UIFont boldSystemFontOfSize:40];
timerLabel.text = @”Ready”;
timerLabel.textColor = UIColorHex(0xA6A6A6);
timerLabel.textAlignment = 1;
timerLabel.layer.cornerRadius = 10;
[self.view addSubview:timerLabel];
}
– (void)tick:(NSTimer*)sender
{
time += sender.timeInterval;
if (time > 10.0) {
[sender invalidate];
[UIView animateWithDuration:0.1 animations:^{
timerLabel.transform = CGAffineTransformMakeTranslation(0, –100);
} completion:^(BOOL finished) {
timerLabel.font = [UIFont boldSystemFontOfSize:40];
timerLabel.text = [NSString stringWithFormat:@”%d Steps”, count];
[UIView animateWithDuration:0.1 animations:^{
timerLabel.transform = CGAffineTransformIdentity;
}];
}];
return;
}
timerLabel.font = [UIFont fontWithName:@”DBLCDTempBlack” size:40];
timerLabel.text = [NSString stringWithFormat:@”%.1f”, time];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end