概述


  • 简介

    • CAKeyframeAnimation又称关键帧动画
    • CAKeyframeAnimation是抽象类CAPropertyAnimation的子类,可以直接使用
    • 通过values与path两个属性指定动画属性
  • 注意事项
    • 若指定了path属性,则values属性将被忽略
    • CABasicAnimation相当于只有两个关键帧的CAKeyframeAnimation

关键帧动画的常用属性


  • values(NSArray *)

    • 存放关键帧的多个值
    • 类似于CABasicAnimation的fromValue与toValue值
  • path(CGPathRef)
    • 动画的执行路径
    • 可以通过绘图的方式绘制路径
  • keyTimes(NSArray<NSNumber *> *)
    • 每个关键帧的执行时间
    • 类型为NSNumber类型
    • 若不指定,则所有的关键帧平分动画的duration时长
  • timingFunctions(NSArray<CAMediaTimingFunction *> *)
    • 速度控制函数数组
  • calculationMode(NSString *)
    • 指定关键帧的动画属性
    • 若指定该值,则keyTimes与timingFunctions属性值将被忽略
    • 默认为:kCAAnimationLinear
  • rotationMode(NSString *)
    • 指定旋转模式,默认为nil

示例


  • 效果图

  • 实现思路

    • 通过监听执行动画的UI控件的触摸事件来绘制贝瑟尔曲线
    • 创建关键帧动画,指定执行动画的keyPath属性
    • 将绘制的贝瑟尔曲线作为动画的执行路径
    • 将动画添加到指定的图层上
  • 实现步骤(自定义UIView的子类)

    • 使用成员属性保存贝瑟尔路径
    @property (nonatomic, strong) UIBezierPath *path;
    • 监听触摸事件的状态,绘制贝瑟尔曲线

      • 开始
      //确定起点
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
      //获取当前触摸点
      UITouch *touch = [touches anyObject];
      CGPoint curretnPoint = [touch locationInView:self]; //创建路径
      UIBezierPath *path = [UIBezierPath bezierPath];
      [path moveToPoint:curretnPoint];
      //保存路径
      self.path = path;
      }
      • 移动
      //添加线条
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
      //获取当前触摸点
      UITouch *touch = [touches anyObject];
      CGPoint currentPoint = [touch locationInView:self];
      //添加线条
      [self.path addLineToPoint:currentPoint];
      //重绘,将曲线显示到图层上
      [self setNeedsDisplay];
      }
      • 结束(创建动画)
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
      {
      //创建动画
      CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; //指定执行动画的属性,
      animation.keyPath = @"position";
      //设置动画的执行路径
      animation.path = self.path.CGPath; //设置动画的执行时间
      animation.duration = 1;
      //设置动画的重复次数
      animation.repeatCount = MAXFLOAT; //将动画添加到对应的图层上
      [[[self.subviews firstObject] layer] addAnimation:animation forKey:nil];
      }
    • 将路径显示到图层上

    //绘制路径
    - (void)drawRect:(CGRect)rect
    {
    [self.path stroke];
    }

CoreAnimation-06-CAKeyframeAnimation的更多相关文章

  1. CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation

    - (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...

  2. iOS CoreAnimation详解(一) 有关Layer的动画

    以前由于项目需要 也写了一些动画 ,但是知识不系统,很散.这段时间趁着项目完成的空袭,来跟着大神的脚步系统的总结一下iOS中Core Animation的知识点. 原博客地址:http://blog. ...

  3. iOS CoreAnimation 核心动画

    一 介绍 一组非常强大的动画处理API 直接作用在CALAyer上,并非UIView(UIView动画) CoreAnimation是所有动画的父类,但是不能直接使用,应该使用其子类 属性: dura ...

  4. iOS关于CoreAnimation动画知识总结

    一:UIKit动画 在介绍CoreAnimation动画前先简单介绍一下UIKit动画,大部分简单的动画都可以使用UIKit动画实现,如果想实现更复杂的效果,则需要使用Core Animation了: ...

  5. CoreAnimation方法汇总

    使用CoreAnimation一般分为三个部分:1.创建执行动画的CALayer 2.创建动画 3.CALayer 添加Animation CoreAnimation是以锚点为基础. CoreAnim ...

  6. CoreAnimation笔记

    核心动画继承结构 CoreAnimation Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Ani ...

  7. 【原】iOSCoreAnimation动画系列教程(二):CAKeyFrameAnimation【包会】

    在上一篇专题文章[原]iOSCoreAnimation动画系列教程(一):CABasicAnimation[包会]中我们学习了iOS核心动画CoreAnimation中CABasicAnimation ...

  8. 第二十九篇、CoreAnimation的使用

    使用的的三个步骤 1.初始化演员 2.设置好剧情 3.播放 主要类: CALayer // 绘图部分 CABaseAnimation // 基本动画(缩放,移动) CAKeyframeAnimatio ...

  9. CoreAnimation

    CoreAnimation 1.CABasicAnimation // position CABasicAnimation *ba = [CABasicAnimation animationWithK ...

  10. Core Animation之CAKeyframeAnimation

    在上一篇专题文章中我们学习了iOS核心动画CoreAnimation中CABasicAnimation动画的使用方法.CABasicAnimation已经可以应付一些比较简单的应用场景了,比如view ...

随机推荐

  1. int.class 与 Integer.class

    TYPE 表示的引用类型所对应的基本类型的Class对象!

  2. ubuntu启动器和dash里应用图标不正常

    在以下目录: /usr/share/applications~/.local/share/applications 添加或编辑(pycharm图标不正常):jetbrains-pycharm.desk ...

  3. DDD:小议 BoundexContext 设计

    背景 看了这篇文章:Coding for Domain-Driven Design: Tips for Data-Focused Devs,对 BoundedContext 的设计有了一点新的体会,记 ...

  4. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  5. 初涉SQL Server性能问题(2/4):列出等待资源的会话

    在初涉SQL Server性能问题(1/4)里,我们知道了如何快速检查服务器实例上正运行的任务数和IO等待的任务数.这个是轻量级的脚本,不会给服务器造成任何压力,即使服务器在高负荷下,也可以正常获得结 ...

  6. C#使用基类的引用 and 虚方法和覆写方法

    结论:使用基类的引用,访问派生类对象时,得到的是基类的成员. 虚方法和覆写方法

  7. Winform屏幕截图保存C#代码

    代码如下: using System.Runtime.InteropServices; using System.Drawing.Imaging; [System.Runtime.InteropSer ...

  8. Netty学习之服务器端创建

    一.服务器端开发时序图 图片来源:Netty权威指南(第2版) 二.Netty服务器端开发步骤 使用Netty进行服务器端开发主要有以下几个步骤: 1.创建ServerBootstrap实例 Serv ...

  9. 遇上了artTemplate做的东西

    js现在最牛的地方是 有了Node.js后,前后端的界限几乎都消失了,围绕着它,出现了一整套生态体系. 在生态方面,比php好太多了.

  10. mysql学习笔记 第九天

    order by ,limit 和where子查询的使用 order by: order by 列名1,[列名2],[列名3]...(结果先按列1进行排序,在列1的相同的情况下,再按照列2的排序,以此 ...