// 设定动画类型
// 设定动画类型
// kCATransitionFade 淡化
// kCATransitionPush 推挤
// kCATransitionReveal 揭开
// kCATransitionMoveIn 覆盖
// @"cube" 立方体
// @"suckEffect" 吸收
// @"oglFlip" 翻转
// @"rippleEffect" 波纹
// @"pageCurl" 翻页
// @"pageUnCurl" 反翻页
// @"cameraIrisHollowOpen" 镜头开
// @"cameraIrisHollowClose" 镜头关
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.aLayer = [CALayer layer];
self.aLayer.frame = CGRectMake(20, 20, 280, 300);
self.aLayer.backgroundColor = [UIColor grayColor].CGColor;
[self.view.layer addSublayer:self.aLayer];
self.bLayer = [CALayer layer];
self.bLayer.frame = CGRectMake(40, 40, 40, 40);
self.bLayer.backgroundColor = [UIColor redColor].CGColor;
[self.aLayer addSublayer:self.bLayer];
self.cLayer = [CALayer layer];
self.cLayer.frame = CGRectMake(80, 80, 40, 40);
self.cLayer.backgroundColor = [UIColor blueColor].CGColor;
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 300, 100, 44);
[button addTarget:self action:@selector(tapButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)tapButton
{
CATransition * transition = [CATransition animation];
transition.duration = 3;
transition.type = @"cube";
transition.subtype = kCATransitionFromRight;
SecondViewController * sec = [[SecondViewController alloc]init];
[self.navigationController pushViewController:sec animated:NO];
[self.navigationController.view.layer addAnimation:transition forKey:nil];
}
-(void)tapButton3
{
CAAnimationGroup * group = [CAAnimationGroup animation];
group.duration = 5;
NSMutableArray * arr = [NSMutableArray arrayWithCapacity:2];
CABasicAnimation * transformA = [CABasicAnimation animationWithKeyPath:@"transform"];
// transformA.duration = 3;
CATransform3D start = self.aLayer.transform;
transformA.fromValue = [NSValue valueWithCATransform3D:start];
CATransform3D end = CATransform3DTranslate(start, 100, 100, 0);
transformA.toValue = [NSValue valueWithCATransform3D:end];
[arr addObject:transformA];
CABasicAnimation * bgColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
bgColor.fromValue = (__bridge id)(self.aLayer.backgroundColor);
bgColor.toValue = (__bridge id)([UIColor blueColor].CGColor);
[arr addObject:bgColor];
group.animations = arr;
[self.aLayer addAnimation:group forKey:nil];
}
//图像沿贝塞尔曲线移动
-(void)tapButton2
{
CAKeyframeAnimation * positionK = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionK.duration = 5;
//创建一个圆形路径
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(40, 40, 200, 200)];
positionK.path = path.CGPath;
[self.aLayer addAnimation:positionK forKey:nil];
}
//图像的移动
-(void)tapButton1
{
CAKeyframeAnimation * positionK = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionK.duration = 10;
NSMutableArray * values = [NSMutableArray arrayWithCapacity:5];
CGPoint start = self.aLayer.position;
[values addObject:[NSValue valueWithCGPoint:start]];
CGPoint point1 = start;
point1.x += 200;
[values addObject:[NSValue valueWithCGPoint:point1]];
CGPoint point2 = point1;
point2.y += 200;
[values addObject:[NSValue valueWithCGPoint:point2]];
CGPoint point3 = point2;
point3.x -= 200;
[values addObject:[NSValue valueWithCGPoint:point3]];
CGPoint end = point3;
end.y -= 200;
[values addObject:[NSValue valueWithCGPoint:end]];
positionK.values = values;
//数组里面元素是NSNumber类型,而且值为0-1之间,递增,
//每两个值之间的差值为动画时间比例
positionK.keyTimes = [NSArray arrayWithObjects:@0,@0.1,@0.4,@0.5,@1, nil];
[self.aLayer addAnimation:positionK forKey:nil];
}
-(void)tapButton
{
CABasicAnimation * positionA = [CABasicAnimation animationWithKeyPath:@"transform"];
positionA.duration = 3;
CATransform3D start = self.aLayer.transform;
positionA.fromValue = [NSValue valueWithCATransform3D:start];
CATransform3D end = CATransform3DRotate(start, 3.14, 1, 0, 0);
positionA.toValue = [NSValue valueWithCATransform3D:end];
[self.aLayer addAnimation:positionA forKey:nil];
}