ひらがなパネルの中に、動物の名前が隠れているよ!という感じで、ことばをさがす子供の勉強iPhoneアプリを作ってみます。今回かくれんぼしてくれる動物は、きりん、うし、りす、いるかの4匹にしてみました。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
問題を表示するところで、NSArrayとして、言葉と画像を用意しています。ランダムで表示する問題を決定して、対応する番号のものを使うようにしています。パネルは横8枚、縦8枚の計64枚を配置しています。一カ所は答えが出るように、パネルの入れ替えを一度行っています。その際にNSStringをloopで一文字ずつ抜き出して、パネルの文字を入れ替えるのですが、charactorAtIndexはunicharを返してくるので、stringWithFormatの@”%C”を使いNSStringに変換しています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSString *question;
UIImageView *questionImageView;
NSMutableArray *selected;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createQuestion];
[self createWordPanels];
}
– (void)createQuestion
{
NSArray *words = @[@”きりん“, @”うし“, @”りす“, @”いるか“];
// wordsに対応した画像を適当に用意
NSArray *images = @[@”a.png”, @”b.png”, @”c.png”, @”d.png”];
int rand = arc4random() % 4;
question = [words objectAtIndex:rand];
// initialize
[questionImageView removeFromSuperview];
questionImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:rand]]];
questionImageView.frame = CGRectMake(50, 50, 220, 100);
questionImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:questionImageView];
}
– (void)createWordPanels
{
NSArray *hiragana = [@”あ い う え お か き く け こ さ し す せ そ た ち つ て と な に ぬ ね の は ひ ふ へ ほ ま み む め も や ゆ よ わ を ん“ componentsSeparatedByString:@” “];
// initialize
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UILabel class]]) {
[v removeFromSuperview];
}
}
for (int i=0; i<64; i++) {
float x = (i % 8) * 40.0;
float y = (i / 8) * 40.0 + 228.0;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 40, 40)];
l.font = [UIFont systemFontOfSize:20];
l.text = [hiragana objectAtIndex:arc4random() % [hiragana count]];
l.textAlignment = NSTextAlignmentCenter;
l.layer.cornerRadius = 3;
l.layer.borderWidth = 1;
l.layer.borderColor = self.view.backgroundColor.CGColor;
l.tag = i + 1;
[self.view addSubview:l];
}
// 答えを一カ所、強制的にはめる
// 右方向 or 下方向に
int direction = arc4random() % 2; // 0:vertical 1:horizontal
if (direction == 0) {
// 下方向
int x = arc4random() % 7;
int y = arc4random() % (7 – question.length);
// replace panels
for (int i=0; i<question.length; i++) {
int tag = x + y * 8 + 1;
UILabel *l = (UILabel*)[self.view viewWithTag:tag];
l.text = [NSString stringWithFormat:@”%C”, [question characterAtIndex:i]];
y++;
}
} else {
// 右方向
int x = arc4random() % (7 – question.length);
int y = arc4random() % 7;
// replace panels
for (int i=0; i<question.length; i++) {
int tag = x + y * 8 + 1;
UILabel *l = (UILabel*)[self.view viewWithTag:tag];
l.text = [NSString stringWithFormat:@”%C”, [question characterAtIndex:i]];
x++;
}
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//最初の一枚
selected = [[NSMutableArray alloc] init];
CGPoint p = [[touches anyObject] locationInView:self.view];
UIView *target;
for (UIView *v in self.view.subviews) {
if (CGRectContainsPoint(v.frame, p) && v.tag > 0) {
target = v;
}
}
target.backgroundColor = [UIColor greenColor];
[selected addObject:target];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
UIView *target;
for (UIView *v in self.view.subviews) {
if (CGRectContainsPoint(v.frame, p) && v.tag > 0) {
target = v;
}
}
if (target && ![target isEqual:[selected lastObject]]) {
target.backgroundColor = [UIColor greenColor];
[selected addObject:target];
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// check
NSMutableString *s = [@”” mutableCopy];
for (UILabel *l in selected) {
[s appendString:l.text];
}
if ([s isEqual:question]) {
[UIView animateWithDuration:0.3 animations:^{
questionImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
int count = 0;
for (UIView *v in self.view.subviews) {
[self performSelector:@selector(removeView:) withObject:v afterDelay:0.01 * count];
count++;
}
// restart
[self performSelector:@selector(createQuestion) withObject:nil afterDelay:1.0];
[self performSelector:@selector(createWordPanels) withObject:nil afterDelay:1.0];
}];
} else {
for (UIView *v in self.view.subviews) {
if (v.tag > 0) {
v.backgroundColor = [UIColor whiteColor];
}
}
}
}
– (void)removeView:(UIView*)v
{
[v removeFromSuperview];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end