核心动画CoreAnimation:2-动画的实现步骤
动画的实现步骤
当我们需要创建一个动画时,通常可以按照如下步骤进行:
- 实例化一个CAAnimation的子类对象,例如CABasicAnimation、CAKeyframeAnimation、CATransition等;
- 设置动画对象的动画属性;
- 添加动画对象到一个CALayer类的对象上,并播放动画。
简单示例
我们通过一个简单的示例,介绍一下动画的实现步骤,该示例实现了对一个视图对象的layer进行平移,在平移的过程中播放动画。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//初始化CALayer对象,并添加到控制器view的layer上
CALayer *subLayer = [CALayer layer];
subLayer.frame = CGRectMake(140, 100, 100, 100);
subLayer.backgroundColor = [UIColor yellowColor].CGColor;
subLayer.borderColor = [UIColor redColor].CGColor;
subLayer.borderWidth = 4.0;
subLayer.cornerRadius = 2.0;
[self.view.layer addSublayer:subLayer];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//1-实例化CABasicAnimation对象
CABasicAnimation *animation = [CABasicAnimation animation];
//2-设置动画属性
animation.keyPath = @"position";
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(290, 150)];
animation.duration = 2.0;
//3-添加动画对象到一个CALayer类的对象上,播放动画
[subLayer addAnimation:animation forKey:nil];
});
}
示例代码
https://github.com/99ios/14.4.2
文章发布时间为: December 15th , 2016 at 11:40 am
最后编辑时间为: September 15th , 2017 at 03:54 am
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 15th , 2017 at 03:54 am
本文由 99ios 创作,转载请注明出处