• CoreAnimation-07-CAAnimationGroup


    概述


    • 简介
      • CAAnimationGroup又称组动画或动画组
      • 将多个动画放到动画组中,并赋值给layer的animations属性,动画组中所有动画就会并发执行
    • 注意事项
      • 动画组中的动画不会被压缩,超出动画时长的部分将会被剪掉
      • 动画组中的动画的delegate与removedOnCompletion属性将会被忽略
      • 由于忽略了removedOnCompletion属性,动画结束layer会恢复到动画前的状态

    CAAnimationGroup唯一的属性


    • animations(NSArray<CAAnimation *> *)
      • 存放并发执行的所有动画
      • 数组元素为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];
      }
      
  • 相关阅读:
    Java获取Linux系统cpu使用率
    jsoup 提取 html 中的所有链接、图片和媒体
    使用Spring定时任务并且通过AOP监控任务执行情况
    MySQL导出数据库、数据库表结构、存储过程及函数【用】
    linux下部署一个JavaEE项目的简单步骤
    MySQL 错误日志(Error Log)
    linux下程序JDBC连接不到mysql数据库
    linux下mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”)的处理方法
    Spring Boot的核心
    项目中菜单折叠问题
  • 原文地址:https://www.cnblogs.com/theDesertIslandOutOfTheWorld/p/4774791.html
Copyright © 2020-2023  润新知