ひらがな火山からでてくる「あいうえお」をたくさん捕まえよう!という感じで、ひらがなを勉強するiPhoneアプリの作り方を書いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
2種類のタイマー、「ひらがな作成」、「ひらがな移動」を用意して、0.2秒に一度ひらがなを火山からバクハツさせて、ひらがな移動タイマーのなかで、上に飛ばしています。飛ばす向きは、ひらがな作成時にtransformで回転をかけておいて、移動タイマーで transformのtranslateでy方向のみの数値を変更、これで左右に分かれて飛ぶようにしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *targets;
@property (nonatomic, strong) UIView *board;
@end
@implementation ViewController
@synthesize targets;
@synthesize board;
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[self createVolcano];
[self createBoard];
[self startTimer];
}
– (void)createVolcano
{
UIImage *image = [UIImage imageNamed:@”volcano”];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
iv.frame = CGRectMake(0, 250, 320, 250);
[self.view addSubview:iv];
}
– (void)createBoard
{
board = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 50)];
board.backgroundColor = [UIColor blackColor];
[self.view addSubview:board];
targets = [[NSMutableArray alloc] init];
NSArray *words = [@”あ い う え お“ componentsSeparatedByString:@” “];
for (int i=0 ; i<4; i++) {
NSString *w = [words objectAtIndex:arc4random() % [words count]];
float x = 50 * i;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(x, 5, 50, 50)];
l.text = w;
l.font = [UIFont boldSystemFontOfSize:40];
l.textAlignment = 1;
l.textColor = [UIColor yellowColor];
l.tag = i + 10;
l.backgroundColor = [UIColor clearColor];
[board addSubview:l];
[targets addObject:l];
}
}
– (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(eruption) userInfo:nil repeats:YES];
}
– (void)eruption
{
NSArray *words = [@”あ い う え お“ componentsSeparatedByString:@” “];
NSString *word = [words objectAtIndex:arc4random() % [words count]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
label.text = word;
label.textAlignment = 1;
label.font = [UIFont boldSystemFontOfSize:50];
label.textColor = [UIColor redColor];
label.backgroundColor = [UIColor colorWithWhite:1 alpha:0.01];
[self.view insertSubview:label atIndex:0];
label.center = CGPointMake(160, 400);
float direction = M_PI * 0.01 * (arc4random() % 40) – M_PI * 0.2;
label.transform = CGAffineTransformMakeRotation(direction);
[NSTimer scheduledTimerWithTimeInterval:1.5/60.0 target:self selector:@selector(move:) userInfo:label repeats:YES];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchWord:)];
[label addGestureRecognizer:tap];
}
– (void)move:(NSTimer*)sender
{
UILabel *word = sender.userInfo;
word.transform = CGAffineTransformTranslate(word.transform, 0, –4);
if ([[word.layer valueForKeyPath:@”translate.translation.y”] intValue] < –300) {
[sender invalidate];
}
}
– (void)touchWord:(UITapGestureRecognizer*)gr
{
NSString *word = ((UILabel*)gr.view).text;
// last check
// ボードのひらがなが3つ赤に変わっていたら、次でクリア
int redCounter = 0;
for (int i=0; i<4; i++) {
UILabel *l = [self.targets objectAtIndex:i];
if ([l.textColor isEqual:[UIColor redColor]]) {
redCounter++;
}
}
// check board
for (int i=0; i<4; i++) {
UILabel *l = [self.targets objectAtIndex:i];
if ([l.textColor isEqual:[UIColor yellowColor]] && [l.text isEqual:word]) {
l.textColor = [UIColor redColor];
if (redCounter == 3) {
[self showNext];
}
return;
}
}
}
– (void)showNext
{
targets = nil;
[UIView animateWithDuration:0.2 animations:^{
board.transform = CGAffineTransformMakeTranslation(-300, 0);
} completion:^(BOOL finished) {
[board removeFromSuperview];
[self createBoard];
}];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end