首页 > 学院 > 开发设计 > 正文

OC-26.CAAnimationGroup

2019-11-14 17:55:04
字体:
来源:转载
供稿:网友

概述


  • 简介

    • CAAnimationGroup又称组动画或动画组
    • 将多个动画放到动画组中,并赋值给layer的animations属性,动画组中所有动画就会并发执行
  • 注意事项

    • 动画组中的动画不会被压缩,超出动画时长的部分将会被剪掉
    • 动画组中的动画的delegate与removedOnCompletion属性将会被忽略
    • 由于忽略了removedOnCompletion属性,动画结束layer会恢复到动画前的状态

CAAnimationGroup唯一的属性


  • animations(NSArray *)

    • 存放并发执行的所有动画
    • 数组元素为CAAnimation的子类

示例


  • 效果图

  • 实现思路

    • 创建动画组对象group
    • 创建多个CABasicAnimation对象,分别作用与不通过的属性
    • 将多个基本动画对象赋值给group的animations属性
    • 将动画组添加到要执行动画的图层上
    • 通过隐式动画改变图层的背景颜色
  • 实现步骤

    • 通过storyboard创建UIView控件,并拥有它,修改它的class属性为自定义的UIView的子类
    @PRoperty (weak, nonatomic) IBOutlet UIView *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 = [self randomColor].CGColor;
    • 实现返回随机颜色的方法
    //返回随机颜色- (UIColor *)randomColor{	return [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1.0];}

     

 
 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表