Layer层核心动画-CAAnimation
2016-06-03 本文已影响1747人
dogs_five
这篇文章主要对layer层动画CAAnimation
进行一个详解.
CAAnimation是一个抽象类,我们不直接使用,我们使用的是其子类。
CAAnimation三个子类:
CAPropertyAnimation: CABasicAnimation(toValue/fromValue)
CAKeyFrameAnimation(values/path)
CAtransition(type/subtype)
CAGroupAnimation(animations)
小括号中的是完成动画的核心属性,通过对属性的设置可以设置我们需要的动画效果。
接下来上代码创建一个layer,通过storyboard添加几个按钮
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)CALayer *layer;
@property (strong, nonatomic) IBOutlet UIView *viewA;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self settingLayer];
}
//创建一个layer对象
//真正用来展示视图的是layer,但是layer没有用户交互。
//透明度用opacity而不是alpha
//中心点用position表示而不是center
- (void)settingLayer{
//1 创建layer对象
self.layer = [[CALayer alloc]init];
//2 设置属性
//a frame
self.layer.frame = CGRectMake(50, 600, 200, 200);
//b 颜色
self.layer.backgroundColor = [UIColor redColor].CGColor;
//c 阴影
self.layer.shadowColor = [UIColor blueColor].CGColor;
//d 阴影偏移量
self.layer.shadowOffset = CGSizeMake(40, 40);
//e 阴影透明度
// self.layer.shadowOpacity = 0.6;
//f 设置layer的图片
self.layer.contents = (id)[UIImage imageNamed:@"1.jpg"].CGImage;
//g 中心点
self.layer.position = CGPointMake(50, 550);
//h 锚点(取值范围[0,1],锚点相对于父视图的原点距离恒定,默认值(0.5,0.5))
self.layer.anchorPoint = CGPointMake(0, 0.5);
//3 添加到layer层上
[self.view.layer addSublayer:self.layer];
}
模拟器效果图.png
#pragma mark CATransition
- (IBAction)transition:(id)sender {
//1 创建动画对象
CATransition *trans = [CATransition animation];
//2 设置动画属性
trans.duration = 3;
trans.repeatCount = HUGE_VAL;
//3 设置动画效果(立方体效果)
trans.type = @"cube";
trans.subtype = @"fromLeft";
//4 开始动画
[self.layer addAnimation:trans forKey:nil];
}
CATransition效果 图.png
CATransition的属性值.png
通过设置type和subtype可以产生不同的效果,上图就是不同的属性值。这里也可以添加手势,通过滑动换图片类似于相册。
#pragma mark property之BasicAnimation改变位置
- (IBAction)basicPosition:(id)sender {
//1 创建动画对象,参数决定动画效果,不能随便设置
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position"];
//2 设置属性
basic.duration = 1.5;
basic.repeatCount = HUGE_VAL;
basic.autoreverses = YES;
basic.toValue = [NSValue valueWithCGRect:CGRectMake(300, 250, 100, 100)];
//3 添加动画
[self.layer addAnimation:basic forKey:nil];
}
#pragma mark property之BasicAnimation改变大小
- (IBAction)basicFrame:(id)sender {
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
basic.duration = 1;
basic.repeatCount = HUGE_VAL;
basic.autoreverses = YES;
//设置动画之后的效果
[basic setRemovedOnCompletion:NO];
basic.fillMode = kCAFillModeForwards;
//动画最终大小
basic.toValue = [NSValue valueWithCGRect:CGRectMake(50, 50, 250, 250)];
[self.layer addAnimation:basic forKey:nil];
}
#pragma mark property之关键帧动画values
- (IBAction)keyFrameValue:(id)sender {
//1 创建动画对象,参数决定动画效果,不能随便设置
CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
//2 设置属性
key.duration = 1;
key.repeatCount = HUGE_VAL;
key.autoreverses = YES;
//3 设置动画效果
CGFloat c = self.layer.position.x;
CGFloat l = self.layer.position.x - 50;
CGFloat r = self.layer.position.x + 50;
//移动轨迹(可以随意给)
key.values = @[@(c),@(l),@(c),@(r),@(r+100)];
//4 添加动画
[self.layer addAnimation:key forKey:nil];
}
#pragma mark property之关键帧动画贝塞尔曲线
- (IBAction)keyFramePath:(id)sender {
//1 创建动画对象,参数决定动画效果,不能随便设置
CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//2设置动画属性
key.duration = 2;
key.repeatCount = 1;
[key setRemovedOnCompletion:NO];
//3 设置动画路径
//1 创建路径
CGMutablePathRef path = CGPathCreateMutable();
//2 设置起始点
CGPathMoveToPoint(path, nil, 50, 600);
//3路径值可以随意设置,图片移动会跟随提供的值而变化
CGPathAddCurveToPoint(path, nil, 10, 200, 350, 500,10, 10);
//4设置曲线路径
key.path = path;
//4 添加动画
[self.layer addAnimation:key forKey:nil];
}
最后一个动画 CAGroupAnimation(animations)
没有写。它其实就是将几个动画效果融合在一起通过annimations这个参数,譬如,一边移动位置一遍改变大小。