足すと道路に書いてある数字になるように、数字を道路においていくという感じの子供向けiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (strong, nonatomic) NSMutableArray *tiles;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor darkGrayColor];
[self createTiles];
[self createNumbers];
// title
UILabel *title = [[UILabel alloc] init];
title.backgroundColor = [UIColor clearColor];
title.text = @”Addition Road”;
title.textColor = [UIColor whiteColor];
title.font = [UIFont systemFontOfSize:40];
[title sizeToFit];
title.center = CGPointMake(160, 40);
[self.view addSubview:title];
}
– (void)createTiles
{
self.tiles = [[NSMutableArray alloc] init];
NSArray *answers = @[@2, @5, @6, @10, @15, @24];
for (int i=0; i<6; i++) {
float x = 160;
float y = 410 – 60 * i;
UILabel *t = [self createTile];
t.center = CGPointMake(x, y);
t.text = [NSString stringWithFormat:@”%d”, [[answers objectAtIndex:i] intValue]];
if (i>0) {
t.tag = [[answers objectAtIndex:i] intValue] – [[answers objectAtIndex:i-1] intValue];
} else {
t.tag = [[answers objectAtIndex:i] intValue];
}
[self.tiles addObject:t];
}
}
– (void)createNumbers
{
NSArray *answers = @[@2, @1, @3, @9, @5, @4];
for (int i=0; i<6; i++) {
float x = 50 * i + 35;
float y = 480;
UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 40, 40)];
number.center = CGPointMake(x, y);
number.text = [NSString stringWithFormat:@”%d”, [[answers objectAtIndex:i] intValue]];
number.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
number.textColor = [UIColor greenColor];
number.font = [UIFont systemFontOfSize:40];
number.textAlignment = NSTextAlignmentCenter;
number.layer.cornerRadius = 20;
[self.view addSubview:number];
number.userInteractionEnabled = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[number addGestureRecognizer:pan];
}
}
– (void)move:(UIPanGestureRecognizer*)gr
{
CGPoint p = [gr locationInView:self.view];
CGPoint distance = {p.x – gr.view.center.x, p.y – gr.view.center.y};
gr.view.transform = CGAffineTransformMakeTranslation(distance.x, distance.y);
if (gr.state == UIGestureRecognizerStateEnded) {
// check correct
UIView *target = [self.view viewWithTag:[[(UILabel*)gr.view text] intValue]];
if (CGRectContainsPoint(target.frame, p)) {
gr.view.center = p;
gr.view.userInteractionEnabled = NO;
target.backgroundColor = [UIColor brownColor];
[(UILabel*)target setText:@””];
}
gr.view.transform = CGAffineTransformIdentity;
}
}
– (UILabel*)createTile
{
UILabel *tile = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 50)];
tile.layer.borderColor = [UIColor grayColor].CGColor;
tile.layer.borderWidth = 2;
tile.backgroundColor = [UIColor whiteColor];
tile.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:tile];
return tile;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end