最近、子供が何となく数字を覚えてきた気がするので、
もう一押し、と思い立ちこんなアプリを作ってみた。
すうじをタッチすると、上から四角い何かが降ってきて、
4種の楽器でドレミファソがなるという仕様。
環境
XcodeのiOS6 iPhone Simulatorで動かしています。
ポイント
counterという配列を1〜5までのボタンを押した回数を
格納しておくために準備しておく。
落ちてくる四角に目と口をつけて、顔の向きを変える。
ドレミを鳴らす楽器を切り替える、
というところで押した回数を利用。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController () {
int counter[5];
AVAudioPlayer *player;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBlueStripeBack];
[self createButtons];
}
– (void)createBlueStripeBack
{
self.view.backgroundColor = [UIColor whiteColor];
for (int i=0; i<50; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, i*10, 320, 5)];
line.backgroundColor = [UIColor colorWithRed:0.8 green:1 blue:1 alpha:0.5];
[self.view addSubview:line];
}
}
– (void)createButtons
{
for (int i=1; i<=5; i++) {
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
btn.layer.cornerRadius = 5;
btn.backgroundColor = [UIColor colorWithRed:0 green:0.7 blue:1 alpha:1.0];
btn.center = CGPointMake(i*60 – 20, 400);
btn.tag = i;
[self.view addSubview:btn];
UILabel *num = [[UILabel alloc] init];
num.text = [NSString stringWithFormat:@”%d”, i];
num.font = [UIFont fontWithName:@”MarkerFelt-Wide” size:50];
num.textAlignment = 1;
num.textColor = [UIColor colorWithRed:0.8 green:1 blue:1 alpha:1.0];
num.backgroundColor = [UIColor clearColor];
[num sizeToFit];
num.center = CGPointMake(25,25);
[btn addSubview:num];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[btn addGestureRecognizer:tap];
}
}
– (void)tap:(UIGestureRecognizer*)gr
{
CGPoint p = gr.view.center;
int num = gr.view.tag;
counter[num-1] = (counter[num-1] + 1) % 4;
[self play:num];
for (UIView *old in self.view.subviews) {
if (old.tag == num + 10) {
[UIView animateWithDuration:1.0 animations:^{
old.transform = CGAffineTransformMakeScale(1.5, 0.2);
old.alpha = 0.1;
} completion:^(BOOL finished) {
[old removeFromSuperview];
}];
}
}
for (int i=0; i<num; i++) {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
v.tag = num + 10;
v.backgroundColor = [UIColor colorWithRed:0 green:0.7 blue:1 alpha:1.0];
v.center = CGPointMake(p.x, –30);
float angle = M_PI/2.0 * counter[num-1];
v.transform = CGAffineTransformMakeRotation(angle);
[self.view addSubview:v];
UIView *eye1 = [[UIView alloc] initWithFrame:CGRectMake(5, 10, 5, 5)];
eye1.backgroundColor = [UIColor whiteColor];
[v addSubview:eye1];
UIView *eye2 = [[UIView alloc] initWithFrame:CGRectMake(20, 10, 5, 5)];
eye2.backgroundColor = [UIColor whiteColor];
[v addSubview:eye2];
UIView *mouth = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 10, 5)];
mouth.backgroundColor = [UIColor whiteColor];
[v addSubview:mouth];
[UIView animateWithDuration:1.0 delay:0.5*i options:UIViewAnimationOptionCurveEaseOut animations:^{
v.center = CGPointMake(p.x, p.y – 40 * (i+1) – 20);
} completion:^(BOOL finished) {
}];
}
}
– (void)play:(int)count
{
if ([player isPlaying]) {
[player stop];
}
NSArray *names = [NSArray arrayWithObjects:@”sax125″, @”flute125″, @”bell125″, @”marimba125″, nil];
NSString *name = [names objectAtIndex:counter[count-1]];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@”mp3″];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[player prepareToPlay];
[player play];
[self performSelector:@selector(stop:) withObject:player afterDelay:0.5 * count – 0.05];
}
– (void)stop:(id)obj
{
if ([player isEqual:obj]) {
[player stop];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end