CoreAnimation-06-CAKeyframeAnimation
概述
- 简介
- 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的更多相关文章
- CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation
- (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...
- iOS CoreAnimation详解(一) 有关Layer的动画
以前由于项目需要 也写了一些动画 ,但是知识不系统,很散.这段时间趁着项目完成的空袭,来跟着大神的脚步系统的总结一下iOS中Core Animation的知识点. 原博客地址:http://blog. ...
- iOS CoreAnimation 核心动画
一 介绍 一组非常强大的动画处理API 直接作用在CALAyer上,并非UIView(UIView动画) CoreAnimation是所有动画的父类,但是不能直接使用,应该使用其子类 属性: dura ...
- iOS关于CoreAnimation动画知识总结
一:UIKit动画 在介绍CoreAnimation动画前先简单介绍一下UIKit动画,大部分简单的动画都可以使用UIKit动画实现,如果想实现更复杂的效果,则需要使用Core Animation了: ...
- CoreAnimation方法汇总
使用CoreAnimation一般分为三个部分:1.创建执行动画的CALayer 2.创建动画 3.CALayer 添加Animation CoreAnimation是以锚点为基础. CoreAnim ...
- CoreAnimation笔记
核心动画继承结构 CoreAnimation Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Ani ...
- 【原】iOSCoreAnimation动画系列教程(二):CAKeyFrameAnimation【包会】
在上一篇专题文章[原]iOSCoreAnimation动画系列教程(一):CABasicAnimation[包会]中我们学习了iOS核心动画CoreAnimation中CABasicAnimation ...
- 第二十九篇、CoreAnimation的使用
使用的的三个步骤 1.初始化演员 2.设置好剧情 3.播放 主要类: CALayer // 绘图部分 CABaseAnimation // 基本动画(缩放,移动) CAKeyframeAnimatio ...
- CoreAnimation
CoreAnimation 1.CABasicAnimation // position CABasicAnimation *ba = [CABasicAnimation animationWithK ...
- Core Animation之CAKeyframeAnimation
在上一篇专题文章中我们学习了iOS核心动画CoreAnimation中CABasicAnimation动画的使用方法.CABasicAnimation已经可以应付一些比较简单的应用场景了,比如view ...
随机推荐
- ruby -- 问题解决(四)编码错误导致无法显示(2)
从数据库中取得数据显示时报 incompatible character encodings: GBK and ASCII-8BIT或 incompatible character encodings ...
- sql order by 排序多个字段
order by 多个字段,每个字段后面都有排序方式,默认ASC 例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc
- LoopBack – 开源的,可扩展的 Node.js 框架
LoopBack 是建立在 Express 基础上的开源 Node.js 框架,专门为 Mobile,Web 和其他设备做了优化.LoopBack 能够连接到多个数据源,使用 Node.js 编写业务 ...
- NoSuchMethodError: antlr.collections.AST.getLine()I
错误完整表述: Filter execution threw an exception] with root cause java.lang.NoSuchMethodError: antlr.coll ...
- 移动端页面使用单位的问题:关于px、百分比、em、rem开发中逐渐转换的问题记录
开始写前端页面也有了快两年时间,从一开始的懵逼到现在的淡定,但是不能改变我还是一只小菜鸟的事实,平时遇到的一些问题都会记录在文件夹里,现在都整理一下大家一起分享自己平时也翻翻看看~ 不知道大家平时写的 ...
- [Architect] Abp 框架原理解析(5) UnitOfWork
本节目录 介绍 分析Abp源码 实现UOW 介绍 UOW(全称UnitOfWork)是指工作单元. 在Abp中,工作单元对于仓储和应用服务方法默认开启.并在一次请求中,共享同一个工作单元. 同时在Ab ...
- 看看如何面试前端工程师:Github很重要
从程序员的角度提出要去学习哪些知识,下面这篇文章从面试官的角度介绍到面试时可能会问到的一些问题.不过我想先给你们一个忠告,招聘是一件非常艰巨的任务,在45分钟内指出一名侯选人是否合适是你需要完成的任务 ...
- WampServer 给电脑搭建apache服务器和php环境
WampServer 给电脑搭建apache服务器和php环境 前端不仅要做页面展示层,还负责着数据交互的部分,不要等到后端人员做好工作了前端才开始对接,那样太被动了. 前端在完成静态页面的编码后,就 ...
- ok6410 android driver(4)
Install busybox for goldfish/phone 1. Download busybox source code http://www.busybox.net/ 2. Decomp ...
- 控制网页的Panel是否显示
在网页上有十二个Panel控件,默认状态是不显示的,根据当前月作为条件去控制对应的Panel控件显示. Insus.NET以下使用三种方法来实现它,先是第一种,使用FindControl方法 第二种方 ...