ひらがな「さしすせそ」をかくれんぼするように見せて、おなじものを選ぶiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (strong, nonatomic) NSString *selected;
@property (strong, nonatomic) UILabel *topPiece;
@property (strong, nonatomic) AVAudioPlayer *mySound;
@end
@implementation ViewController
@synthesize topPiece;
– (void)viewDidLoad
{
[super viewDidLoad];
[self createPuzzle];
}
– (void)createPuzzle
{
//init
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
NSArray *words = @[@”さ“, @”し“, @”す“, @”せ“, @”そ“];
self.selected = [words objectAtIndex:arc4random() % 5];
topPiece = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 240, 240)];
topPiece.layer.cornerRadius = 120;
topPiece.backgroundColor = [UIColor purpleColor];
topPiece.text = self.selected;
topPiece.textColor = [UIColor orangeColor];
topPiece.font = [UIFont systemFontOfSize:180];
topPiece.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:topPiece];
UIView *topMask = [[UIView alloc] initWithFrame:CGRectMake(0, 120, 240, 120)];
topMask.backgroundColor = [UIColor purpleColor];
[topPiece addSubview:topMask];
CABasicAnimation *wave = [CABasicAnimation animationWithKeyPath:@”transform.translation.y”];
wave.fromValue = @(–30);
wave.toValue = @30;
wave.autoreverses = YES;
wave.repeatCount = 100;
wave.duration = 1.0;
[topMask.layer addAnimation:wave forKey:nil];
for (int i=0; i<5; i++) {
NSString *s = [words objectAtIndex:i];
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
float x = 55 * i + 15;
float y = 320 + (i%2) * 70;
b.frame = CGRectMake(x, y, 70, 70);
b.backgroundColor = [UIColor orangeColor];
b.tintColor = [UIColor purpleColor];
b.titleLabel.font = [UIFont systemFontOfSize:30];
b.layer.cornerRadius = 35;
[b setTitle:s forState:UIControlStateNormal];
[self.view addSubview:b];
[b addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
}
}
– (void)push:(UIButton*)sender
{
sender.userInteractionEnabled = NO;
[self sound:sender.titleLabel.text];
if([self.selected isEqual:sender.titleLabel.text]) {
[[self.topPiece.subviews[0] layer] removeAllAnimations];
[UIView animateWithDuration:1.0 animations:^{
[self.topPiece.subviews[0] setAlpha:0];
} completion:^(BOOL finished) {
[self performSelector:@selector(createPuzzle) withObject:nil afterDelay:2.0];
}];
} else {
[sender setTitle:@”x” forState:UIControlStateNormal];
}
}
– (void)sound:(NSString*)s
{
NSArray *kakiku = @[@”さ“, @”し“, @”す“, @”せ“, @”そ“];
int i = [kakiku indexOfObject:s];
NSArray *names = @[@”sa”,@”si”,@”su”,@”se”,@”so”];
NSString *fileName = [names objectAtIndex:i];
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:@”m4a”]];
self.mySound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
self.mySound.volume = 0.5;
[self.mySound setVolume:1.0];
[self.mySound play];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end