タッチすると、甲羅の中に手足をしまう亀を表示する
iPhoneアプリのサンプルをつくってみる。
ポイント
手足を、ディスプレイの中心座標に
CGAffineTransformで移動させることで、甲羅の中に隠す。
隠し終わったら、CGAffineTransformIdentityを指定して、
元の座標にもどす。
環境
今回つくった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 blueColor];
[self createTurtle];
}
– (void)createTurtle
{
[self createHead];
UIView *tail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 60)];
tail.layer.cornerRadius = 20;
tail.backgroundColor = [UIColor greenColor];
tail.center = CGPointMake(160, 380);
tail.layer.borderWidth = 2;
tail.tag = 1;
tail.alpha = 0.7;
[self.view addSubview:tail];
for (int i=0; i<4; i++) {
UIView *hand = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
hand.backgroundColor = [UIColor greenColor];
hand.layer.borderWidth = 2;
float angle = i * M_PI / 2.0 + M_PI/4.0;
float x = 100 * cos(angle) + self.view.center.x;
float y = 100 * sin(angle) + self.view.center.y;
hand.center = CGPointMake(x, y);
hand.layer.cornerRadius = 25;
hand.tag = 1;
hand.alpha = 0.7;
[self.view addSubview:hand];
}
UIView *shell = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
shell.backgroundColor = [UIColor greenColor];
shell.layer.cornerRadius = 100;
shell.center = self.view.center;
shell.layer.masksToBounds = YES;
shell.layer.borderColor = [UIColor blackColor].CGColor;
shell.layer.borderWidth = 2;
[self.view addSubview:shell];
// center
UIView *panel = [self createHexagon];
[shell addSubview:panel];
// around
for (int i=0; i<6; i++) {
UIView *p = [self createHexagon];
p.transform = CGAffineTransformMakeRotation(i * M_PI/3.0);
p.transform = CGAffineTransformTranslate(p.transform, 100, 0);
[shell addSubview:p];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[shell addGestureRecognizer:tap];
}
– (void)createHead
{
UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 60)];
head.center = CGPointMake(160, 180);
head.backgroundColor = [UIColor greenColor];
head.layer.cornerRadius = 25;
head.alpha = 0.7;
head.layer.borderWidth = 2;
[self.view addSubview:head];
UIView *eyeR = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
eyeR.backgroundColor = [UIColor blackColor];
eyeR.layer.cornerRadius = 2.5;
eyeR.center = CGPointMake(37, 13);
[head addSubview:eyeR];
UIView *eyeL = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
eyeL.backgroundColor = [UIColor blackColor];
eyeL.layer.cornerRadius = 2.5;
eyeL.center = CGPointMake(13, 13);
[head addSubview:eyeL];
head.tag = 1;
}
– (UIView*)createHexagon
{
UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
panel.center = CGPointMake(100, 100);
panel.backgroundColor = [UIColor clearColor];
CGPoint p1 = CGPointMake(0, 75);
CGPoint p2 = CGPointMake(0, 25);
CGPoint p3 = CGPointMake(50, 0);
CGPoint p4 = CGPointMake(100, 25);
CGPoint p5 = CGPointMake(100, 75);
CGPoint p6 = CGPointMake(50, 100);
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:p1];
[path addLineToPoint:p2];
[path addLineToPoint:p3];
[path addLineToPoint:p4];
[path addLineToPoint:p5];
[path addLineToPoint:p6];
[path closePath];
CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:panel.layer];
sl.fillColor = [UIColor greenColor].CGColor;
sl.strokeColor = [UIColor blackColor].CGColor;
sl.lineWidth = 3;
sl.path = path.CGPath;
[panel.layer addSublayer:sl];
return panel;
}
– (void)tap:(UITapGestureRecognizer*)gr
{
for (UIView *v in self.view.subviews) {
float dx = self.view.center.x – v.center.x;
float dy = self.view.center.y – v.center.y;
[UIView animateWithDuration:1.0 animations:^{
v.transform = CGAffineTransformMakeTranslation(dx, dy);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
v.transform = CGAffineTransformIdentity;
}];
}];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end