CABasicAnimation
2015-11-12 本文已影响4439人
iOS_成才录
一、简介
- CABasicAnimation是
CAPropertyAnimation
的子类, CAPropertyAnimation有一个字符串类型的keyPath
属性
![](https://img.haomeiwen.com/i831339/b143eab37d0ec973.png)
-
keyPath内容是CALayer的可动画Animatable属性
,可动画属性可见CAlayer篇 -
我们可以指定CALayer的某个属性名为keyPath,并且对CALayer的这个属性的值进行修改,达到相应的动画效果。
- 例如:指定keyPath = @"position",就会修改CALayer的position属性的值,- > 可以实现平移的动画效果
-
属性说明: fromValue:keyPath相应属性的初始值,ntoValue:keyPath相应属性的结束值
-
因此,初始化好CAPropertyAnimation的子类对象后,必须先设置keyPath(修改的是CALayer的哪个属性)-> 指明执行的是怎样的动画(平移/缩放/旋转等)
动画过程说明:
- 随着动画的进行,在长度为
duration
的持续时间内,keyPath
相应属性的值从fromValue
渐渐地变为toValue
-
keyPath
内容是CALayer的可动画Animatable
属性 - 如果
fillMode=kCAFillModeForwards
同时removedOnComletion=NO
,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
二、应用
1、缩放动画
-
多种方案,这里实现三种就。
-
方案一:CABasicAnimation animationWithKeyPath:@"bounds"
- layer会从原来的尺寸变为30x30
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
anim.duration = 2;
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 30, 30)];
[_myView.layer addAnimation:anim forKey:nil];
- 方案二:CABasicAnimation animationWithKeyPath:@"transform"
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5; // 动画持续1.5s
// CALayer的宽度从0.5倍变为2倍
// CALayer的高度从0.5倍变为1.5倍
anim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 1.5, 1)];
[_myView.layer addAnimation:anim forKey:nil];
- 方案三:anim.keyPath = @"transform.scale";
// 创建CABasicAnimation
CABasicAnimation *anim = [CABasicAnimation animation];
// 告诉系统修改图层的哪个属性
anim.keyPath = @"transform.scale";
// 告诉系统修改图层的哪个值
// anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
anim.toValue = @0.5;
// 取消反弹
// 告诉在动画结束的时候不要移除
anim.removedOnCompletion = NO;
// 始终保持最新的效果
anim.fillMode = kCAFillModeForwards;
[_redView.layer addAnimation:anim forKey:nil];
2、平移动画
-
多种方式实现
-
2.1方式一:CABasicAnimation -> anim.keyPath = @"position";
- 注意:该方式动画执行完毕后,并没有真正改变CALayer的position属性的值,
一切都是假象,并不会真实修改layer的属性
,不会平移到(250,500)
- 注意:该方式动画执行完毕后,并没有真正改变CALayer的position属性的值,
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 500)];
// 必须设置代理
anim.delegate = self;
// 取消反弹
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[_redView.layer addAnimation:anim forKey:nil];
- 2.2 CABasicAnimation -> animationWithKeyPath:@"transform"
- 注意:通过CALayer的transform属性实现平移动画,layer会从自己的初始位置平移到(350, 350)位置
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1;
CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
anim.toValue = [NSValue valueWithCATransform3D:form];
[_myView.layer addAnimation:anim forKey:nil];
- 2.3 _redView.layer.position :修改layer的可动画属性,UIView 的方法animateWithDuration:.......执行动画
- 该方式,实现了真正的平移到(250,500)
[UIView animateWithDuration:0.25 animations:^{
_redView.layer.position = CGPointMake(250, 500);
} completion:^(BOOL finished) {
NSLog(@"%@", NSStringFromCGPoint(_redView.layer.position));
}];
3、旋转动画
- 这里就例举一种实现方案了。
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1.5;
// 绕着(0, 0, 1)这个向量轴 Z 轴,顺时针旋转45°(M_PI_4)
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
[_redView.layer addAnimation:anim forKey:nil];