之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样。

一般使用的时候  首先通过 animationWithKeyPath 方法 创建一个CAKeyframeAnimation实例,

CAKeyframeAnimation 的一些比较重要的属性

1. path

这是一个 CGPathRef  对象,默认是空的,当我们创建好CAKeyframeAnimation的实例的时候,可以通过制定一个自己定义的path来让  某一个物体按照这个路径进行动画。这个值默认是nil  当其被设定的时候  values  这个属性就被覆盖

2. values

一个数组,提供了一组关键帧的值,  当使用path的 时候 values的值自动被忽略。

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, 50.0, 120.0);

CGPathAddLineToPoint(path, NULL, 90, 130);

CGPathAddLineToPoint(path, NULL, 40, 140);

CGPathAddLineToPoint(path, NULL, 80, 150);

CGPathAddLineToPoint(path, NULL, 30, 160);

CGPathAddLineToPoint(path, NULL, 100, 170);

//CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);   曲线path

CAKeyframeAnimation * animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

[animation setPath:path];

[animation setDuration:2.0f];

CFRelease(path);

[_btn.layer addAnimation:animation forKey:NULL];

以上代码就是让  _btn 按照上面path中定义的5个点进行移动,其中  的@"position"代表的是属性。

//////////////////////////////////////////////////////////////////////

例子

A、 B两面, 从A面旋转到B面然后有旋转到A面

代码片段一

CAKeyframeAnimation *theAnimation = [CAKeyframeAnimationanimation];

theAnimation.values = [NSArray arrayWithObjects:

[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(0, 0,1,0)],

[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.14/2.0f, 0,1,0)],

[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.14, 0,1,0)],

[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.14 + 3.14/2.0f, 0,1,0)],

[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.14 + 3.14, 0,1,0)],

nil];

theAnimation.cumulative = YES;

theAnimation.duration = 2.0f;

theAnimation.repeatCount = 1;

theAnimation.removedOnCompletion = YES;

[_headImageView.layer addAnimation:theAnimation forKey:@"transform"];

&&&&&&&&&&&&&

代码片段二

_headImageView.animationDuration = 2;

_headImageView.animationImages = [NSArrayarrayWithObjects:[UIImageimageNamed:@"A.jpg"],

[UIImage imageNamed:@"B.jpg"],[UIImage imageNamed:@"B.jpg"],

[UIImage imageNamed:@"A.jpg"], nil];

_headImageView.animationRepeatCount = 1;

[_headImageViewstartAnimating];

上面的代码很巧妙的结合在了一起

代码片段一让headImageView旋转了360度,一共分为了4个步骤,也就是每次90度(3.14/2.0),因为在第一个90度的时候,需要从A切换到B,所以这里用到了每90度一个阶段。

代码片段二种使用了同样的时间进行了图片的切换, 4个步骤,分别使用了对应的图片。这样两者结合起来,就达到了一个完美的效果。

真TM是人才啊

CATransform3D myTransform;
myTransform = CATransform3DMakeRotation(angle, x, y, z);

该CATransform3DMakeRotation函数创建了一个转变,将在三维轴坐标系以任意弧度旋转层。x-y-z轴的有个确定的范围(介于-1 和+1之间) 。相应的坐标轴指定的值告诉系统在该轴上旋转。例如,如果X轴是设置为-1或1 ,该对象将的X轴的方向上旋转,这意味着将把它垂直旋转。把这些值看做是插入在图像每个坐标轴上的秸秆(Think of these values as inserting straws through the image for each axis.)。如果秸秆插过x轴,图像将沿着秸秆垂直旋转。您可以使用坐标轴角度值创建更复杂的旋转。。对于大多数的用途,但是,值介于-1和+1已经足够。

/////////////////////////////////////////////////

1.渐渐变淡的效果:

CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"opacity"];

opacityAnimation.duration = 3.0f;

opacityAnimation.values = @[@1, @0.45, @0];

opacityAnimation.keyTimes = @[@0, @0.5, @1];

[_imgView.layer addAnimation:opacityAnimation forKey:nil];

/////////////////////////////////////////////////

2.从大变小

CABasicAnimation *scaleAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale.xy"];

scaleAnimation.fromValue = @1.0;

scaleAnimation.toValue = @0.0;

scaleAnimation.duration = 3.0f;

[_imgView.layer addAnimation:scaleAnimation forKey:nil];

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3.将上面的2个animation 放到一个 animationgroup 里面  就可以一起运行了

CAAnimationGroup * group = [CAAnimationGroupanimation];

group.duration = 3.0f;

group.repeatCount = INFINITY;

group.animations = @[scaleAnimation,opacityAnimation];

[_imgView.layeraddAnimation:group forKey:nil];

CAKeyframeAnimation的更多相关文章

  1. 核心动画 - CAKeyframeAnimation 简单应用

    核心动画: 登录按钮的抖动效果: CAKeyframeAnimation * kfAnimation = [CAKeyframeAnimation animationWithKeyPath:@&quo ...

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

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

  3. 核心动画(CAKeyframeAnimation)

    Main.storyboard ViewController.m // //  ViewController.m //  8A02.核心动画 - CAKeyframeAnimation // //  ...

  4. IOS Animation-CABasicAnimation、CAKeyframeAnimation详解&区别&联系

    1.先看看网上流传的他们的继承图: 从上面可以看出CABasicAnimation与CAKeyframeAnimation都继承于CAPropertyAnimation.而CAPropertyAnim ...

  5. 之二:CAKeyframeAnimation - 关键帧动画

    是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CA ...

  6. IOS第18天(6,CAKeyframeAnimation关键帧动画)

    ******* #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...

  7. IOS 核心动画之CAKeyframeAnimation - iBaby

    - IOS 核心动画之CAKeyframeAnimation - 简单介绍 是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation ...

  8. 用CAKeyframeAnimation构建动画路径

    复杂路径的动画,我们可以借助关键关键帧动画(CAKeyframeAnimation)来实现,给其的path属性设置相应的路径信息即可. 以下为一个红色的小球按照指定的路径运动的动画. 此动画关键在于如 ...

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

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

  10. iOS:核心动画之关键帧动画CAKeyframeAnimation

    CAKeyframeAnimation——关键帧动画 关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是: –CABasicAnimation只能 ...

随机推荐

  1. hdu 4277 USACO ORZ

    没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  2. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  3. lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历

    题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...

  4. Linux实时网络监控工具:iftop

    iftop是类似于top的实时流量监控工具,可以用来实时监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等.夜间值班的童鞋如果发现有邮局流量异常时可以使用该软件查看详细流量状况. 下面介 ...

  5. 编译器的未来——我们还需要C++么?

    在未来我们还需要纯C++开发模式么? 随着C++11的诞生,C++已经越来越臃肿,从03的时候就觉得C++实在是太复杂了.以一个合格C++程序员的标准来简单的来说3-5年略有小成,5-8年才可以说自己 ...

  6. 确认某端口占用情况并结束相应进程(Windows)

    (1)确认某端口是否被占用 (2)通过查找对应的PID号,定位是哪一个进程在使用该端口 (3)通过PID号结束该进程 # 查找端口2000是否被占用C:\Users\tdcqma>netstat ...

  7. Tomcat 自动上传的删除文件

    解决方案: 在 sever.xml 中的 Context 中建一个虚拟路径,指定到服务器硬盘中的其他位置,比如放在 d: 的某个目录下. 同时可以将这个路径映射成为你当前工程名后面带个目录,比如说 / ...

  8. 01-语言入门-01-A+B Problem

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=1    描述 此题为练手用题,请大家计算一下a+b的值   输入 输入两个数,a,b 输 ...

  9. struct dev_t

    device number(dev_t) linux driver 2009-08-21 10:08:03 阅读26 评论0 字号:大中小 dev_t description:     the dev ...

  10. Python3 学习第一弹:基本数据类型

    本人学习主要从<python基础教程第二版>,<dive into python3>等书籍,及一些网上大牛的博客中学习特别是Python官方文档<Python Tutor ...