数字の10の「いち」を折り紙をパタパタするような感じで表示するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic) int counter;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// orange frame
UIView *wf = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 260, 240)];
wf.backgroundColor = [UIColor orangeColor];
[self.view addSubview:wf];
[self createFirstTriangle];
[self createZero];
}
– (void)createFirstTriangle
{
CALayer *l = [self triangle];
l.position = CGPointMake(30, 80);
}
– (void)createZero
{
UILabel *l = [[UILabel alloc] init];
l.text = @”0″;
l.font = [UIFont boldSystemFontOfSize:220];
[l sizeToFit];
l.center = CGPointMake(160, 150);
[self.view addSubview:l];
}
– (CALayer *)triangle
{
CGPoint p[] = {CGPointMake(0, 32), CGPointMake(32, 0), CGPointMake(32, 32)};
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i=0; i<3; i++) {
(i==0) ? [path moveToPoint:p[i]]
: [path addLineToPoint:p[i]];
}
[path closePath];
CAShapeLayer *triangle = [CAShapeLayer layer];
triangle.frame = CGRectMake(0, 0, 32, 32);
triangle.path = path.CGPath;
triangle.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.3].CGColor;
triangle.strokeColor = [UIColor grayColor].CGColor;
triangle.lineWidth = 1;
triangle.zPosition = 50;
[self.view.layer addSublayer:triangle];
return triangle;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.counter++;
switch (self.counter) {
case 1:
[self flipTypeA:CGPointMake(30, 80)];
break;
case 2:
[self flipTypeB:CGPointMake(62, 80)];
break;
case 3:
[self flipTypeC:CGPointMake(62, 112)];
break;
case 4:
[self flipTypeD:CGPointMake(62, 112)];
break;
case 5:
[self flipTypeE:CGPointMake(62, 144)];
break;
case 6:
[self flipTypeB:CGPointMake(62, 144)];
break;
case 7:
[self flipTypeC:CGPointMake(62, 176)];
break;
case 8:
[self flipTypeD:CGPointMake(62, 176)];
break;
case 9:
[self flipTypeE:CGPointMake(62, 208)];
break;
default:
break;
}
}
– (void)flipTypeA:(CGPoint)a
{
CALayer *l = [self triangle];
[self.view.layer addSublayer:l];
l.anchorPoint = CGPointMake(1.0, 0.5);
l.position = CGPointMake(a.x + 16, a.y);
CATransform3D t = CATransform3DIdentity;
t = CATransform3DRotate(t, M_PI, 0, –1, 0);
l.transform = t;
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform”];
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
anim.toValue = [NSValue valueWithCATransform3D:l.transform];
anim.cumulative = YES;
anim.duration = 0.3;
[l addAnimation:anim forKey:nil];
}
– (void)flipTypeB:(CGPoint)a
{
CALayer *l = [self triangle];
[self.view.layer addSublayer:l];
l.anchorPoint = CGPointMake(0.5, 1.0);
l.position = CGPointMake(a.x, a.y + 16);
CATransform3D t0 = CATransform3DMakeRotation(M_PI, 0, 1, 0);
l.transform = CATransform3DRotate(t0, M_PI, 1, 0, 0);
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform”];
anim.fromValue = [NSValue valueWithCATransform3D:t0];
anim.toValue = [NSValue valueWithCATransform3D:l.transform];
anim.duration = 0.3;
[l addAnimation:anim forKey:nil];
}
– (void)flipTypeC:(CGPoint)a
{
CALayer *l = [self triangle];
[self.view.layer addSublayer:l];
l.position = CGPointMake(a.x, a.y);
CATransform3D t0 = CATransform3DMakeRotation(M_PI, –1, 1, 0);
l.transform = CATransform3DIdentity;
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform”];
anim.fromValue = [NSValue valueWithCATransform3D:t0];
anim.toValue = [NSValue valueWithCATransform3D:l.transform];
anim.duration = 0.3;
[l addAnimation:anim forKey:nil];
}
– (void)flipTypeD:(CGPoint)a
{
CALayer *l = [self triangle];
[self.view.layer addSublayer:l];
l.anchorPoint = CGPointMake(0.5, 1.0);
l.position = CGPointMake(a.x, a.y + 16);
CATransform3D t0 = CATransform3DIdentity;//MakeRotation(M_PI, 0, 1, 0);
l.transform = CATransform3DRotate(t0, M_PI, 1, 0, 0);
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform”];
anim.fromValue = [NSValue valueWithCATransform3D:t0];
anim.toValue = [NSValue valueWithCATransform3D:l.transform];
anim.duration = 0.3;
[l addAnimation:anim forKey:nil];
}
– (void)flipTypeE:(CGPoint)a
{
CALayer *l = [self triangle];
[self.view.layer addSublayer:l];
l.position = CGPointMake(a.x, a.y);
CATransform3D t0 = CATransform3DMakeRotation(-M_PI/2.0, 0, 0, 1);
l.transform = CATransform3DRotate(t0, M_PI, –1, 1, 0);
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform”];
anim.fromValue = [NSValue valueWithCATransform3D:t0];
anim.toValue = [NSValue valueWithCATransform3D:l.transform];
anim.duration = 0.3;
[l addAnimation:anim forKey:nil];
}
@end