しろの道路をなぞって車を動かそう!
スタートの青い丸から、ゴールのオレンジ丸まで
たどりつけるかな?
という感じの子供向けiPhoneゲームのサンプルコードを書いてみた。
ポイント
道路になるUIViewは、あらかじめ、NSArrayに入れておきます。
hitTestをつかって、指でなぞったところに、道路のViewがあるか確認
という風にして、道路があるところだけ車を動かすようにしています。
UIPanGestureRecognizerのキャンセルは enable = NO
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSMutableArray *streetArr;
UILabel *start;
UILabel *goal;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
streetArr = [[NSMutableArray alloc] init];
[self createStreet];
[self createStart];
[self createGoal];
[self createCar];
}
– (void)createStart
{
start = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
start.layer.cornerRadius = 40;
start.center = CGPointMake(160, 40);
start.font = [UIFont boldSystemFontOfSize:20];
start.text = @”START”;
start.textColor = [UIColor whiteColor];
start.textAlignment = 1;
start.backgroundColor = [UIColor blueColor];
start.userInteractionEnabled = YES;
[self.view addSubview:start];
[streetArr addObject:start];
}
– (void)createGoal
{
goal = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
goal.layer.cornerRadius = 40;
goal.center = CGPointMake(160, 420);
goal.font = [UIFont boldSystemFontOfSize:20];
goal.text = @”GOAL”;
goal.textColor = [UIColor whiteColor];
goal.textAlignment = 1;
goal.backgroundColor = [UIColor orangeColor];
goal.userInteractionEnabled = YES;
[self.view addSubview:goal];
[streetArr addObject:goal];
}
– (void)createStreet
{
CGRect streetRect[] = {
CGRectMake(140, 60, 40, 120),
CGRectMake(20, 180, 280, 40),
CGRectMake(20, 200, 40, 60),
CGRectMake(260, 200, 40, 120),
CGRectMake(60, 280, 240, 40),
CGRectMake(60, 320, 40, 100),
CGRectMake(60, 400, 80, 40),
};
for (int i=0; i<sizeof(streetRect)/sizeof(CGRect); i++) {
UIView *street = [[UIView alloc] initWithFrame:streetRect[i]];
street.backgroundColor = [UIColor whiteColor];
[self.view addSubview:street];
[streetArr addObject:street];
}
}
– (void)createCar
{
UIImage *img = [UIImage imageNamed:@”car”];
UIImageView *car = [[UIImageView alloc] initWithFrame:CGRectMake(155, 0, 50, 50)];
car.image = img;
car.userInteractionEnabled = YES;
[self.view addSubview:car];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveCar:)];
[car addGestureRecognizer:pan];
}
– (void)moveCar:(UIPanGestureRecognizer*)gr
{
CGPoint p = [gr locationInView:self.view];
if (gr.state == UIGestureRecognizerStateBegan) {
gr.view.transform = CGAffineTransformMakeScale(2, 2);
} else if (gr.state == UIGestureRecognizerStateCancelled) {
gr.view.transform = CGAffineTransformIdentity;
}
BOOL inStreet = NO;
for (UIView *street in streetArr) {
CGPoint testp = [gr locationInView:street];
if([street hitTest:testp withEvent:nil]) {
inStreet = YES;
}
}
if (inStreet) {
gr.view.center = p;
} else {
gr.enabled = NO;
gr.enabled = YES;
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end