简介
基础动画主要提供了对CALayer可变属性进行简单动画的操作。比如:位移、透明度、缩放、旋转、背景色等等。
使用 CABaseAnimation 基本思路就是设置好要调整的属性、初始值、结束值、插值模式等,然后将动画加入到相应的layer上去。
CABasicAnimation可以设定keyPath的起点值,终点值,动画会沿着设定点进行移动。 CABasicAnimation可以看成是只有两个关键点的特殊CAKeyFrameAnimation。
属性
属性 | 解释 |
---|
fromValue | keyPath对应的初始值 |
toValue | keyPath对应的结束值 |
byValue | 所改变属性相同起始值的改变量 |
keyPath 属性
属性 | 解释 |
---|
transform | 3D变换,[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 0)],直接进行3D变换 |
transform.scale | 缩放,@(1.5),放大1.5倍 |
transform.scale.x | 宽度缩放,@(1.5),宽放大1.5倍 |
transform.scale.y | 高度缩放,@(1.5),高放大1.5倍 |
transform.rotation.x | 围绕x轴旋转,@(M_PI),x轴旋转180度 |
transform.rotation.y | 围绕y轴旋转,@(M_PI),y轴旋转180度 |
transform.rotation.z | 围绕z轴旋转,@(M_PI),z轴旋转180度 |
position | 位置(中心点的改变),[NSValue valueWithCGPoint:CGPointMake(100, 100)],中心点变为(100,100) |
opacity | 透明度,@(0.5),透明度变为0.5 |
bounds | 大小的改变,中心点保持不变,[NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)],大小变为(300,300) |
cornerRadius | 圆角的设置 ,@(5),圆角设置为5 |
backgroundColor | 背景颜色变换,(id)[UIColor redColor].CGColor,背景改为红色contents,可以改变layer展示的图片,(id)[UIImage imageNamed:@“12.png”].CGImage,将UIView的展示图片改为12.png |
strokeStart | 从起始点开始变化,fromValue = 0,toValue = 1,为CAShapeLayer的属性 |
strokeEnd | 从结束的位置开始变化,fromValue = 1,toValue = 0.5,为CAShapeLayer的属性 |
path | 根据路径来改变 |
rotaion.x | 旋转,弧度,X轴 |
rotaion.y | 旋转,弧度,Y轴 |
rotaion.z | 旋转,弧度,Z轴 |
rotaion | 旋转,弧度,Z轴,完全等同于rotation.z |
contents | 内容,比如UIImageView的图片 imageAnima.toValue = (id)[UIImage imageNamed:@“to”].CGImage; |
opacity | 透明度 如@(0.7) |
contentsRect.size.width | 横向拉伸缩放 @(0.4)最好是0~1之间的 |
margin | 布局 |
zPosition | 翻转 |
borderWidth | 边框宽 |
frame | 大小位置 |
hidden | 显示隐藏 |
mask | 遮罩 |
masksToBounds | 蒙版 |
shadowColor | 阴影颜色 |
shadowOffset | 阴影偏移量 |
shadowOpacity | 阴影不透明的 |
shadowRadius | 阴影半径 |
实践
位置动画
self.animationView = [[UIView alloc]init];
self.animationView.frame = CGRectMake(0, kHeight/2-50, 50, 50);
self.animationView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.animationView];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(16, 16)];
animation.toValue = [NSValue valueWithCGPoint:self.view.center];
animation.duration = 1.0;
animation.fillMode = kCAFillModeBoth;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
animation.repeatCount = HUGE_VALF;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.animationView.layer addAnimation:animation forKey:@"positonAnimation"];
背景颜色动画
self.animationView = [[UIView alloc]init];
self.animationView.frame = CGRectMake(0, kHeight/2-50, 50, 50);
self.animationView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.animationView];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
animation.fromValue = (id)[UIColor cyanColor].CGColor;
animation.toValue = (id)[UIColor magentaColor].CGColor;
animation.duration = 1.0;
animation.fillMode = kCAFillModeBoth;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
animation.repeatCount = HUGE_VALF;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.animationView.layer addAnimation:animation forKey:@"backgroundColorAnimation"];