核心动画CoreAnimation:7-CAAnimationGroup动画组
CAAnimationGroup动画组,顾名思义就是可以创建一组动画对象,在动画播放时,图层可以同时播放多种动画的叠加效果,例如,我们可以在图层平移过程中同时修改图层的透明度。通过CAAnimationGroup类的对象,我们可以实现复杂动画的播放效果。
CAAnimationGroup常用属性
在CAAnimationGroup类中,最核心的一个属性就是animations属性,这是一个数组,可以在该数组中保存多个动画对象,例如,可以保存若干个CABasicAnimation对象。
@property(nullable, copy) NSArray<CAAnimation *> *animations;
CAAnimationGroup代码示例
示例代码中,创建了一个CAAnimationGroup对象,在该动画组对象中,保存了两个基本动画(CABasicAnimation)对象。可以同时实现对图层的平移以及修改透明度。
- 在控制器类中添加一个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.1 实例化CABasicAnimation对象
CABasicAnimation *animation1 = [CABasicAnimation animation];
//1.2 设置动画属性
animation1.keyPath = @"transform.translation.x";
animation1.toValue = @100;
//2.1 实例化CABasicAnimation对象
CABasicAnimation *animation2 = [CABasicAnimation animation];
//2.2 设置动画属性
animation2.keyPath = @"opacity";
animation2.fromValue = @1.0;
animation2.toValue = @0.1;
//3.1 实例化CAAnimationGroup对象
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//3.2 设置动画组属性
animationGroup.animations = @[animation1,animation2];
animationGroup.duration = 2.0;
//4. 添加动画组对象到一个CALayer类的对象上,播放动画
[self.myLayer addAnimation:animationGroup forKey:nil];
}
初始状态与动画过程中图层的样式对比。
示例代码
https://github.com/99ios/14.4.7
文章发布时间为: December 15th , 2016 at 11:42 am
最后编辑时间为: September 15th , 2017 at 03:55 am
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 15th , 2017 at 03:55 am
本文由 99ios 创作,转载请注明出处