之二:CAKeyframeAnimation - 关键帧动画
是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值,就可以实现CALayer的某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样。
几个关键属性:
- values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
- path:设置一个CGPathRef\CGMutablePathRef的路径对象,默认nil,让CALayer跟着路径移动。path只对CALayer的anchorPoint(锚点)和position(位置)起作用。如果你设置了path,那么values将被忽略
- keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的
说明:CABasicAnimation可看做是最多只有2个关键帧的CAKeyframeAnimation
- Values方式
// 创建5个关键帧的点对象
CGPoint p1 = CGPointMake(, );
CGPoint p2 = CGPointMake(, );
CGPoint p3 = CGPointMake(, );
CGPoint p4 = CGPointMake(, );
CGPoint p5 = CGPointMake(, ); // 将创建的关键帧放入数组中,用NSValue结构体类型转化成对象
NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:p1], [NSValue valueWithCGPoint:p2], [NSValue valueWithCGPoint:p3], [NSValue valueWithCGPoint:p4], [NSValue valueWithCGPoint:p5], nil]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // 给Values属性赋值
[animation setValues:values]; // 动画完成后保持最新状态
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; animation.duration = 4.0; // 动画定时函数属性
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; [self.myView.layer addAnimation:animation forKey:nil];
- 第23行:timingFunction 用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏
- kCAMediaTimingFunctionLinear 线性,即匀速
- kCAMediaTimingFunctionEaseIn 先慢后快
- kCAMediaTimingFunctionEaseOut 先快后慢
- kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
- kCAMediaTimingFunctionDefault 实际效果是动画中间比较快.
- 注意:当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
* + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
* - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
- Path方式:
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGMutablePathRef myPath = CGPathCreateMutable(); // 将路径的起点定位到(50, 120)
CGPathMoveToPoint(myPath, NULL, 50.0, 120.0); // 添加5条直线的路径到myPath中
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , );
CGPathAddLineToPoint(myPath, NULL, , ); // 添加4条曲线路径到myPath中
CGPathAddCurveToPoint(myPath,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
CGPathAddCurveToPoint(myPath,NULL,350.0,275.0,450.0,275.0,130.0,120.0); //以"position"为关键字创建 实例
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // 是否动画回到原位
[animation setAutoreverses:YES]; // 设置path属性
animation.path = myPath; // 释放路径
CGPathRelease(myPath); animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; animation.duration = 4.0; animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.delegate=self; [self.myView.layer addAnimation:animation forKey:nil];
- 其他属性
- calculationMode 这个属性用来设定 关键帧中间的值是怎么被计算的
之二:CAKeyframeAnimation - 关键帧动画的更多相关文章
- core Animation之CAKeyframeAnimation(关键帧动画)
CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...
- iOS之CAKeyframeAnimation关键帧动画详解
CABasicAnimation算是CAKeyFrameAnimation的 特殊情况,即不考虑中间变换过程,只考虑起始点与目标点就可以了.而CAKeyFrameAnimation则更复杂一些,允许我 ...
- IOS第18天(6,CAKeyframeAnimation关键帧动画)
******* #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...
- iOS:核心动画之关键帧动画CAKeyframeAnimation
CAKeyframeAnimation——关键帧动画 关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是: –CABasicAnimation只能 ...
- Silverlight动画的基本知识、关键帧动画
基础知识 (一)动画:是快速播放一系列图像(其中每个图像与下一个图像略微不同)给人造成的一种幻觉 (二)动画类型:两类 (1)From/To/By动画:在起始值和结束值之间进行动画处理. ...
- CSS3过渡动画&关键帧动画
一.过渡动画 过渡(transition)动画,就是从初始状态过渡到结束状态这个过程中所产生的动画. 所谓的状态就是指大小.位置.颜色.变形(transform)等等这些属性. Note:不是所有属性 ...
- Swift - 使用CAKeyframeAnimation实现关键帧动画
1,CAKeyframeAnimation介绍 CAKeyframeAnimation可以实现关键帧动画,这个类可以实现某一属性按照一串的数值进行动画,就像是一帧一帧的制作出来一样. 2,使用样例 ...
- iOS开发UI篇—核心动画(关键帧动画)
转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...
- ios基础动画、关键帧动画、动画组、转场动画等
概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...
随机推荐
- 测试Servlet生命周期学习笔记
测试环境:windows xp旗舰版 软件环境:myclipse8.5+tomcat7.0 ****************************************************** ...
- Windows Azure HandBook (2) Azure China提供的服务
<Windows Azure Platform 系列文章目录> 对于传统的自建数据中心,从底层的Network,Storage,Servers,Virtualization,中间层的OS, ...
- Elasticsearch增删改查 之 —— Get查询
GET API是Elasticsearch中常用的操作,一般用于验证文档是否存在:或者执行CURD中的文档查询.与检索不同的是,GET查询是实时查询,可以实时查询到索引结果.而检索则是需要经过处理,一 ...
- JavaScript客户端MVC 框架综述
简介 15 年前,许多人都使用 Perl 和 ColdFusion 之类的工具构建网站.我们经常编写可以在页面顶部查询数据库的脚本,对数据应用必要的转换,以及在同一个脚本底部显示数据.这类架构适合于向 ...
- 7 款免费的 Metro UI 模板
#1 – Free Metro Ui Style template by Asif Aleem 很棒的蓝色调 Metro UI 管理模板 #2: Metro-Bootstrap by TalksLab ...
- 基于HTML5的Drag and Drop生成图片Base64信息
HTML5的Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag ...
- 【读书笔记】--SQL基础概念复习
主键:每个表,只能有一个主键,主键不能为NULL,且必须是唯一的.主键最好是由单个列组成,但这不是必须的,主键也可以是由多个列组成,如果表的两个列组合在一起能唯一标识一个行,而单个列不能,则可以将这两 ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- SQLite中文排序
定义一个类: using System.Data.SQLite; namespace DAL { /// <summary> /// SQLite中文排序 /// </summary ...
- 新学C++的for,switch和随机数
通过一个小程序学习: #include<iostream> #include<cstdlib> #include<ctime> using namespace st ...