「あか」と「みどり」のちいさい四角をあつめよう。
あかはうえの「あかい」のまるに、みどりはしたの「みどり」のまるに!
という感じの、幼児教育アプリのサンプルコードです。
ポイント
フィールドに、赤い円、緑の円、緑四角の配列、赤四角の配列を用意して、
PanGestureの終了時に仲間分けが出来たか確認しています。
UIviewが円の中に入っているかどうかは、中心座標の距離で判定しました。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *answerA;
UIView *answerB;
NSMutableArray *shapeA;
NSMutableArray *shapeB;
int count;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createAnswerField];
count = 3;
[self createShapes];
}
– (void)createAnswerField
{
answerA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
answerA.layer.cornerRadius = 100;
answerA.center = CGPointMake(160, 0);
answerA.backgroundColor = [self getColorAtIndex:2];
[self.view addSubview:answerA];
answerB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
answerB.layer.cornerRadius = 100;
answerB.center = CGPointMake(160, self.view.bounds.size.height);
answerB.backgroundColor = [self getColorAtIndex:3];
[self.view addSubview:answerB];
}
– (void)createShapes
{
NSMutableArray *positions = [[NSMutableArray alloc] init];
shapeA = [[NSMutableArray alloc] init];
shapeB = [[NSMutableArray alloc] init];
for (int i=0; i<20; i++) {
float x = (i % 5) * 50 + 50;
float y = (i / 5) * 50 + 130;
[positions addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
}
for (int i=0; i<count; i++) {
UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
if(arc4random() % 2) {
shape.backgroundColor = [self getColorAtIndex:1];
[shapeA addObject:shape];
} else {
shape.backgroundColor = [self getColorAtIndex:4];
[shapeB addObject:shape];
}
int pIndex = arc4random() % [positions count];
shape.center = [[positions objectAtIndex:pIndex] CGPointValue];
[positions removeObjectAtIndex:pIndex];
[self.view addSubview:shape];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[shape addGestureRecognizer:pan];
}
}
– (void)pan:(UIPanGestureRecognizer*)gr
{
gr.view.center = [gr locationInView:self.view];
if (gr.state == UIGestureRecognizerStateEnded) {
// check
BOOL clear = YES;
for (UIView *v in shapeA) {
if (![self circleIntersectsA:v B:answerA]) {
clear = NO;
}
}
for (UIView *v in shapeB) {
if (![self circleIntersectsA:v B:answerB]) {
clear = NO;
}
}
// clear effect
if (clear) {
UILabel *v = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
v.backgroundColor = [UIColor whiteColor];
v.text = @”Clear”;
v.textAlignment = 1;
v.font = [UIFont boldSystemFontOfSize:30];
v.transform = CGAffineTransformMakeScale(1, 0);
[self.view addSubview:v];
[UIView animateWithDuration:0.5 animations:^{
v.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
v.transform = CGAffineTransformMakeScale(1, 1);
} completion:^(BOOL finished) {
[v removeFromSuperview];
// next challenge
count++;
[self createShapes];
}];
}];
}
}
}
– (BOOL)circleIntersectsA:(UIView*)a B:(UIView*)b
{
float x = a.center.x – b.center.x;
float y = a.center.y – b.center.y;
float R = 100; // big circle
float r = 15; // small circle
if ((R + r) > hypot(x, y)) {
return YES;
}
return NO;
}
#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
– (UIColor*)getColorAtIndex:(int)i
{
switch (i) {
case 0:
return UIColorHex(0x420A07);
break;
case 1:
return UIColorHex(0xDB352E);
break;
case 2:
return UIColorHex(0x8E1510);
break;
case 3:
return UIColorHex(0x07421C);
break;
default:
return UIColorHex(0x108E3D);
break;
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end