设为首页 加入收藏

TOP

OC - 26.CAAnimationGroup
2017-10-11 16:34:07 】 浏览:7287
Tags:26.CAAnimationGroup

概述


  • 简介

    • 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]; }

     

 
 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS应用发布中的一些细节 下一篇swift-UINavigationController纯..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目