ローマすうじ、しってるかな。
1から10まで, i, ii , iii, iv, v, vi, vii, viii, ix, x
と順番にタッチしてみよう。
という感じで、iPhone算数アプリのサンプルコードを書いてみる。
ポイント
ローマ数字をタッチで、大きくアラビア数字を表示するようにしています。
iが押されたら、したに用意した箱の左上に、小さな四角を一つ表示、
次に、iiが押されたら、となりの箱に小さな四角を2つというかんじで、
順番に箱が開くようにしてみました。
数字の表示、非表示は、表示アニメーションの0.5秒後に、performSelectorで
非表示を呼び出しているのですが、その間に別のローマ数字がタッチされたら
NSObjectのcancelPreviousPerformRequestsWithTargetをつかって、
非表示の非同期呼び出しを一度キャンセルしています。
今回、NSMutableArrayの記述に、Objective-C literals ( Literal Syntax )の書き方をつかっています。NSArrayは通常だと、
NSArray *someArray =
[NSArray arrayWithObjects:firstObject, secondObject, thirdObject, nil];
という書き方ですが、これを使うと、
NSArray *someArray = @[firstObject, secondObject, thirdObject];
という感じです。NSNumberも @1, @2 という書き方ができます。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
動作イメージをyoutubeに上げてあります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
int count;
}
@property (nonatomic, strong) NSMutableArray *numbers;
@property (nonatomic, strong) UILabel *showWindow;
@property (nonatomic, strong) NSMutableArray *boxes;
@end
@implementation ViewController
@synthesize numbers = _numbers;
@synthesize showWindow = _showWindow;
@synthesize boxes = _boxes;
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self start];
}
– (void)start
{
for (UIView *v in self.view.subviews) {
[v removeFromSuperview];
count = 0;
}
self.showWindow = nil;
self.boxes = nil;
self.numbers = nil;
[self createButtons];
[self createBox];
}
– (NSMutableArray*)numbers
{
if (!_numbers) {
_numbers = [NSMutableArray arrayWithObjects:@”i”, @”ii”, @”iii”, @”iv”, @”v”, @”vi”, @”vii”, @”viii”, @”ix”, @”x”, nil];
}
return _numbers;
}
– (UILabel *)showWindow
{
if (!_showWindow) {
_showWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
_showWindow.center = CGPointMake(160, 200);
_showWindow.layer.cornerRadius = 30;
_showWindow.backgroundColor = [UIColor redColor];
_showWindow.font = [UIFont fontWithName:@”Marker Felt” size:100];
_showWindow.textColor = [UIColor whiteColor];
_showWindow.textAlignment = 1;
_showWindow.alpha = 0;
[self.view addSubview:_showWindow];
}
return _showWindow;
}
– (void)createButtons
{
// random seed
// Objective-C literals
NSMutableArray *seed =[@[ @0, @1, @2, @3, @4, @5, @6, @7, @8, @9] mutableCopy];
for (int i=0; i<[self.numbers count]; i++) {
float r = 120;
float angle = i * (2.0 * M_PI / [self.numbers count]);
float x = r * cos(angle) + 160;
float y = r * sin(angle) + 200;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
// show number
NSNumber *number = [seed objectAtIndex:arc4random() % [seed count]];
[seed removeObject:number];
NSString *str = [self.numbers objectAtIndex:[number intValue]];
label.text = str;
label.tag = [number intValue] + 1;
label.font = [UIFont fontWithName:@”Marker Felt” size:30];
label.center = CGPointMake(x, y);
label.textAlignment = 1;
label.backgroundColor = [UIColor darkGrayColor];
label.layer.borderColor = [UIColor colorWithHue:0.08 * i + 0.1 saturation:1 brightness:1 alpha:1].CGColor;
label.layer.borderWidth = 4;
label.textColor = [UIColor whiteColor];
[self.view addSubview:label];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[label addGestureRecognizer:tap];
}
// reset
UILabel *reset = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
reset.text = @”clear”;
reset.font = [UIFont fontWithName:@”Marker Felt” size:30];
reset.textAlignment = 1;
reset.textColor = [UIColor lightGrayColor];
reset.backgroundColor = [UIColor blackColor];
reset.center = CGPointMake(50, 30);
[self.view addSubview:reset];
reset.userInteractionEnabled = YES;
UITapGestureRecognizer *clear = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(start)];
[reset addGestureRecognizer:clear];
}
– (void)createBox
{
self.boxes = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
float x = (i % 5) * 50 + 40;
float y = (i / 5) * 50 + 360;
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(x, y, 40, 40)];
box.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:box];
[self.boxes addObject:box];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
UILabel *l = (UILabel*)gr.view;
self.showWindow.text = [NSString stringWithFormat:@”%d”, l.tag];
// box open
if (count + 1 == l.tag) {
l.backgroundColor = [UIColor colorWithCGColor:l.layer.borderColor];
UIView *box = [self.boxes objectAtIndex:count];
box.backgroundColor = l.backgroundColor;
for (int i=0; i<count + 1; i++) {
float x = (i % 5) * 7 + 4;
float y = (i / 5) * 20 + 10;
UIView *p = [[UIView alloc] initWithFrame:CGRectMake(x, y, 4, 4)];
p.backgroundColor = [UIColor whiteColor];
[box addSubview:p];
}
count++;
}
// reset animation
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[UIView animateWithDuration:0.5 animations:^{
self.showWindow.alpha = 1.0;
} completion:^(BOOL finished) {
[self performSelector:@selector(hideShowWindow) withObject:nil afterDelay:0.5];
}];
}
– (void)hideShowWindow
{
[UIView animateWithDuration:0.2 animations:^{
self.showWindow.alpha = 0;
}];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end