概述简介
CAAnimationGroup又称组动画或动画组将多个动画放到动画组中,并赋值给layer的animations属性,动画组中所有动画就会并发执行注意事项
动画组中的动画不会被压缩,超出动画时长的部分将会被剪掉动画组中的动画的delegate与removedOnCompletion属性将会被忽略由于忽略了removedOnCompletion属性,动画结束layer会恢复到动画前的状态CAAnimationGroup唯一的属性animations(NSArray *)
存放并发执行的所有动画数组元素为CAAnimation的子类示例效果图
实现思路
创建动画组对象group创建多个CABasicAnimation对象,分别作用与不通过的属性将多个基本动画对象赋值给group的animations属性将动画组添加到要执行动画的图层上通过隐式动画改变图层的背景颜色实现步骤
通过storyboard创建UIView控件,并拥有它,修改它的class属性为自定义的UIView的子类@PRoperty (weak,nonatomic)IBOutletUIView *redView;
创建动画组//创建动画组对象CAAnimationGroup *group = [CAAnimationGroup animation];//设置动画组的执行时间
group.duration =1.0;//创建基本动画对象,用来进行缩放CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath =@"transform.scale";
scale.fromValue = @1.0;
scale.toValue = @0.5;//创建基本动画对象,用来进行旋转CABasicAnimation *rotation = [CABasicAnimation animation];
rotation.keyPath =@"transform.rotation";
rotation.toValue = @(arc4random_uniform(M_PI *2));//创建基本动画对象,用来修改位置CABasicAnimation *position = [CABasicAnimation animation];
position.keyPath =@"position";
position.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200) +20, arc4random_uniform(200) +20)];//将基本动画添加到动画组中
group.animations = @[scale, rotation, position];
将动画组添加到要执行动画的图层上[self.redView.layer addAnimation:group forKey:nil];
通过隐式动画改变layer的背景色self.redView.layer.backgroundColor = [selfrandomColor].CGColor;
实现返回随机颜色的方法//返回随机颜色
- (UIColor *)randomColor
{return [UIColorcolorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255) /255.0blue:arc4random_uniform(255) /255.0alpha:1.0];
}