/*
CAAnimation可分为四种:
1.CABasicAnimation
通过设定起始点,终点,时间,动画会沿着你这设定点进行移动。可以看做特殊的CAKeyFrameAnimation
2.CAKeyframeAnimation
Keyframe顾名思义就是关键点的frame,你可以通过设定CALayer的始点、中间关键点、终点的frame,时间,动画会沿你设定的轨迹进行移动
3.CAAnimationGroup
Group也就是组合的意思,就是把对这个Layer的所有动画都组合起来。PS:一个layer设定了很多动画,他们都会同时执行,如何按顺序执行我到时候再讲。
4.CATransition
这个就是苹果帮开发者封装好的一些动画
*/
/*
演员--->CALayer,规定电影的主角是谁
剧本--->CAAnimation,规定电影该怎么演,怎么走,怎么变换
开拍--->AddAnimation,开始执行
*/
//layer可以有两种用途,二者不互相冲突:一是对view相关属性的设置,包括圆角、阴影、边框等参数;二是实现对view的动画操控。因此对一个view进行core animation动画,本质上是对该view的.layer进行动画操纵。
//缩放动画
-(void)createScale{
//演员初始化
UIView *scaleView = [[UIView alloc] initWithFrame:CGRectMake(50, 60, 80, 40)];
// scaleView.alpha = 0.6;
scaleView.backgroundColor = [UIColor redColor];
[self.view addSubview:scaleView];
CALayer *scaleLayer = [[CALayer alloc] init];
scaleLayer.frame = CGRectMake(-40, 0, 40, 40);
scaleLayer.cornerRadius = 20;
scaleLayer.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.6].CGColor;
[scaleView.layer addSublayer:scaleLayer];
//设定剧本
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//从几倍开始
scaleAnimation.fromValue = [NSNumber numberWithFloat:0];
//结束的时候是几倍大小
scaleAnimation.toValue = [NSNumber numberWithFloat:1];
//动画回放
scaleAnimation.autoreverses = YES;
scaleAnimation.fillMode = kCAFillModeForwards;
//动画次数
scaleAnimation.repeatCount = MAXFLOAT;
//执行一次动画的时间
scaleAnimation.duration = 1;
//开演
[scaleLayer addAnimation:scaleAnimation forKey:nil];
}
- (void)initGroupLayer
{
//演员初始化
CALayer *scaleLayer = [[CALayer alloc] init];
scaleLayer.frame = CGRectMake(40, 180, 80, 80);
scaleLayer.cornerRadius = 5;
scaleLayer.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.6].CGColor;
[self.view.layer addSublayer:scaleLayer];
//设定剧本
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.5];
scaleAnimation.toValue = [NSNumber numberWithFloat:1];
scaleAnimation.autoreverses = YES;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.repeatCount = MAXFLOAT;
scaleAnimation.duration = 1;
CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
moveAnimation.fromValue = [NSValue valueWithCGPoint:scaleLayer.position];
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(320 - 80,
scaleLayer.position.y)];
moveAnimation.autoreverses = YES;
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.repeatCount = MAXFLOAT;
moveAnimation.duration = 2;
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1];
rotateAnimation.toValue = [NSNumber numberWithFloat:6.0 * M_PI];
scaleAnimation.autoreverses = YES;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.repeatCount = MAXFLOAT;
scaleAnimation.duration = 2;
CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation];
groupAnnimation.duration = 2;
groupAnnimation.autoreverses = YES;
groupAnnimation.animations = @[moveAnimation, scaleAnimation, rotateAnimation];
groupAnnimation.repeatCount = MAXFLOAT;
[scaleLayer addAnimation:groupAnnimation forKey:@"groupAnnimation"];
}