核心动画CoreAnimation:4-CABasicAnimiation基本动画
本节的代码创建了一个基本的CABasicAnimation动画,实现了对一个CALayer对象的平移动画。在该示例代码中,我们首先创建了一个CABasicAnimiation类的对象,并且指定动画属性keyPath为transform.translation.x,然后设置了目标值toValue以及其他一些动画属性,最后把该基本动画对象添加到一个CALayer对象上进行播放。
CABasicAnimation类
CABasicAnimation类是使用最简单的核心动画类,我们只需要指定keyPath,然后设置起始值(fromValue)和目标值(toValue),然后再把该动画对象添加到一个CALayer类的对象上,即可实现动画的播放。
- keyPath的初始值,默认情况下为空。当不设置fromValue的值时,动画播放的初始值取自layer对象的keyPath指定属性的当前值
@property(nullable, strong) id fromValue;
- keyPath的目标值,即变动后的值
@property(nullable, strong) id toValue;
- 乘数。当fromValue不为空时,目标值为fromValue 乘以 byValue;当fromValue为空时,目标值为layer对象的keyPath指定属性的当前值乘以byValue
@property(nullable, strong) id byValue;
CABasicAnimiation基本动画示例代码
下方的示例代码中,在控制器类中添加了一个CALayer类型的属性myLayer,并且针对myLayer设置了一个平移的动画。
- 在控制器类中添加一个CALayer类的属性,作为动画播放的layer
@interface ViewController ()
@property (nonatomic, strong) CALayer *myLayer;
@end
- 通过懒加载的方式,设置自定义layer的属性
-(CALayer *)myLayer{
if (_myLayer == nil) {
_myLayer = [CALayer layer];
_myLayer.frame = CGRectMake(140, 100, 100, 100);
_myLayer.backgroundColor = [UIColor yellowColor].CGColor;
_myLayer.borderColor = [UIColor redColor].CGColor;
_myLayer.borderWidth = 4.0;
_myLayer.cornerRadius = 2.0;
}
return _myLayer;
}
- 添加该自定义layer到控制器视图的layer上
- (void)viewDidLoad {
[super viewDidLoad];
[self.view.layer addSublayer:self.myLayer];
}
- 当点击屏幕时,播放平移动画
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1. 实例化CABasicAnimation对象
CABasicAnimation *animation = [CABasicAnimation animation];
//2. 设置动画属性
animation.keyPath = @"transform.translation.x";
animation.toValue = @100;
animation.duration = 2.0;
//3. 添加动画对象到一个CALayer类的对象上,播放动画
[self.myLayer addAnimation:animation forKey:nil];å
}
示例代码
https://github.com/99ios/14.4.4
文章发布时间为: December 15th , 2016 at 11:41 am
最后编辑时间为: September 15th , 2017 at 03:54 am
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 15th , 2017 at 03:54 am
本文由 99ios 创作,转载请注明出处