概述


  • 简介

    • CAKeyframeAnimation又称关键帧动画
    • CAKeyframeAnimation是抽象类CAPropertyAnimation的子类,可以直接使用
    • 通过values与path两个属性指定动画属性
  • 注意事项

    • 若指定了path属性,则values属性将被忽略
    • CABasicAnimation相当于只有两个关键帧的CAKeyframeAnimation

关键帧动画的常用属性


  • values(NSArray *)

    • 存放关键帧的多个值
    • 类似于CABasicAnimation的fromValue与toValue值
  • path(CGPathRef)

    • 动画的执行路径
    • 可以通过绘图的方式绘制路径
  • keyTimes(NSArray *)

    • 每个关键帧的执行时间
    • 类型为NSNumber类型
    • 若不指定,则所有的关键帧平分动画的duration时长
  • timingFunctions(NSArray *)

    • 速度控制函数数组
  • 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];
    }
 
 

OC - 25.CAKeyframeAnimation的更多相关文章

  1. OC基础(25)

    NSNumber NSValue NSDate NSFileManager *:first-child { margin-top: 0 !important; } body > *:last-c ...

  2. OC动画:CAKeyframeAnimation

    // 方法一 用法1​ Value方式 //创建动画对象 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyP ...

  3. 25 (OC)* iOS网络HTTP、TCP、UDP、Socket 知识总结

    应用层:1.用户接口.应用程序:2.Application典型设备:网关:3.典型协议.标准和应用:TELNET.FTP.HTTP 表示层:1.数据表示.压缩和加密presentation2.典型设备 ...

  4. c和oc小知识

    1.const const 修饰了*p1 / *p2 const int * p1=&age; int const * p2=&age;//和上面的意义一样 ,换句话说就是 在 “ * ...

  5. oc集合

    本人之前学习过一年半ios开发 由于行情太过凄惨,故转前端.心在前端,苹果亦难忘!把我平时的笔记作出给大家总结! 回顾之前的知识 便利初始化函数:框架类库中的一些类有一系列的以init开头的方法,这些 ...

  6. OC 面试问题汇总

    OC 问题汇总: 1. 你如何理解 iOS 内存管理   1. new alloc copy retain这些对象我们都要主动的release或者 autorelease   2. 如果是类方法创建的 ...

  7. 关于OC中的小数精确计算---NSDecimalNumber

    NSDecimalNumber 翻译补充自:http://rypress.com/tutorials/objective-c/data-types/nsdecimalnumber 感谢乐于分享的大神 ...

  8. Swift - 使用CAKeyframeAnimation实现关键帧动画

    1,CAKeyframeAnimation介绍 CAKeyframeAnimation可以实现关键帧动画,这个类可以实现某一属性按照一串的数值进行动画,就像是一帧一帧的制作出来一样.   2,使用样例 ...

  9. OC的总结 ***希望对大家有帮助*** ---高小杰

    1.  NSLog           是Foundation提供的一个输出函数,它的功能非常强大,不仅可以输出字符串,还可以输出各种对象,到后面程序还会见到大量的使用NSLog()函数. 2.  N ...

随机推荐

  1. QML学习:Rectangle,Text,TextEdit,Flickable,Flipable元素

    QML学习:Rectangle,Text,TextEdit,Flickable,Flipable元素 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 参 ...

  2. Unity 利用Coroutine实现跳动数字效果

    纯粹转载:转载注明参考链接! 参考链接:http://xataxnova.blog.163.com/blog/static/236620063201451061738122/,作者:网易博客 xata ...

  3. jquery.lazyload的使用

    1.引入 <script src="jquery.js" type="text/javascript"></script> <sc ...

  4. Web Service和ISAPI的区别与联系 转

    Web Service和ISAPI的区别与联系   1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...

  5. windows下protobuf jar包的编译

    0.如果你不想手动编译生成,请直接跳到最后下载附件. 1.下载protobuf release版本:https://github.com/google/protobuf/releases,protoc ...

  6. 关于响应事件中的Sender

    很多响应事件都会有个参数就是Sender,如下: - (IBAction)updateSliderValue:(id)sender Sender其实就是触发响应的那个实例对象,比如这个消息是由一个UI ...

  7. hdu 4750 Count The Pairs(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 代码: #include<cstdio> #include<cstring&g ...

  8. CPP变量参数别名

    1,变量起"绰号"的操作称为引用(reference),"绰号"称为引用名,申明引用的语法格式; 变量数据类型 &引用名 = 已申明的变量名; 和C中的 ...

  9. redis.config翻译

    # Redis configuration file example#redis配置文件范例 # Note on units: when memory size is needed, it is po ...

  10. winform 曲线(贝塞尔) 分类: WinForm 2014-12-29 16:52 109人阅读 评论(0) 收藏

    <span style="font-size:14px;">//覆盖OnPaint事件</span> <span style="font-s ...