青のタワー、赤のタワーをタッチしてみよう。
電波が飛んで隣のタワーの色がかわっちゃう??
という感じで子供向けiPhoneゲームのサンプルコードを書いてみました。
ポイント
タワーの絵は、UIBezierPathを使って一筆書きです。
タップした際に、animationWithDurationで電波が広がるような
感じにアニメーションを追加してみました。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
for (int i=0; i<48; i++) {
UIView *tower = [self createTower];
float x = (i % 6) * 50 + 35;
float y = (i / 6) * 50 + 65;
tower.center = CGPointMake(x, y);
tower.tag = 1;
}
}
– (UIView*)createTower
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 40)];
[path addLineToPoint:CGPointMake(20, 0)];
[path addLineToPoint:CGPointMake(40, 40)];
[path addLineToPoint:CGPointMake(10, 20)];
[path addLineToPoint:CGPointMake(40, 20)];
CAShapeLayer *sl = [[CAShapeLayer alloc] init];
sl.strokeColor = [UIColor blueColor].CGColor;
sl.fillColor = [UIColor clearColor].CGColor;
sl.lineWidth = 2;
sl.path = path.CGPath;
UIView *tower = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
tower.backgroundColor = [UIColor clearColor];
[tower.layer addSublayer:sl];
[self.view addSubview:tower];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(radio:)];
[tower addGestureRecognizer:tap];
return tower;
}
– (void)radio:(UITapGestureRecognizer*)gr
{
UIView *wave = [self createWave:30];
[self.view addSubview:wave];
wave.center = gr.view.center;
wave.alpha = 0.1;
[self chageTowerColor:gr.view];
[UIView animateWithDuration:0.3 animations:^{
wave.alpha = 0.8;
} completion:^(BOOL finished) {
UIView *wave2 = [self createWave:50];
[self.view addSubview:wave2];
wave2.center = gr.view.center;
wave2.alpha = 0.1;
[UIView animateWithDuration:0.3 animations:^{
wave2.alpha = 0.8;
wave.alpha = 0.4;
} completion:^(BOOL finished) {
UIView *wave3 = [self createWave:70];
[self.view addSubview:wave3];
wave3.center = gr.view.center;
wave3.alpha = 0.1;
[UIView animateWithDuration:0.3 animations:^{
[wave removeFromSuperview];
wave2.alpha = 0.4;
wave3.alpha = 0.8;
} completion:^(BOOL finished) {
UIView *wave4 = [self createWave:90];
[self.view addSubview:wave4];
wave4.center = gr.view.center;
wave4.alpha = 0.1;
[UIView animateWithDuration:0.3 animations:^{
[wave2 removeFromSuperview];
wave3.alpha = 0.4;
wave4.alpha = 0.8;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
[wave3 removeFromSuperview];
wave4.alpha = 0.4;
} completion:^(BOOL finished) {
[wave4 removeFromSuperview];
// chage inrange tower
CGRect range = CGRectMake(gr.view.frame.origin.x – 20, gr.view.frame.origin.y – 20, 90, 90);
for (UIView *v in self.view.subviews) {
if (v.tag == 1
&& CGRectIntersectsRect(range, v.frame)
&& v != gr.view) {
[self chageTowerColor:v];
}
}
}];
}];
}];
}];
}];
}
– (UIView*)createWave:(float)R
{
UIView *wave = [[UIView alloc] initWithFrame:CGRectMake(0, 0, R, R)];
wave.backgroundColor = [UIColor clearColor];
wave.layer.cornerRadius = R/2.0;
wave.layer.borderColor = [UIColor purpleColor].CGColor;
wave.layer.borderWidth = 3;
return wave;
}
– (void)chageTowerColor:(UIView*)tower
{
CAShapeLayer *sl = [tower.layer.sublayers objectAtIndex:0];
if (sl.strokeColor != [UIColor blueColor].CGColor) {
sl.strokeColor = [UIColor blueColor].CGColor;
} else {
sl.strokeColor = [UIColor redColor].CGColor;
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end