转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的

iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了。

下面详解各种类型动画的使用方式

1、通过动画上下文使用UIKit动画

  1. -(void)animationOfUIKit
  2. {
  3. UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
  4. redView.backgroundColor=[UIColor redColor];
  5. [self.view addSubview:redView];
  6. //开始动画
  7. [UIView beginAnimations:@"test" context:nil];
  8. //动画时长
  9. [UIView setAnimationDuration:1];
  10. /*
  11. *要进行动画设置的地方
  12. */
  13. redView.backgroundColor=[UIColor blueColor];
  14. redView.frame=CGRectMake(50, 50, 200, 200);
  15. redView.alpha=0.5;
  16. //动画结束
  17. [UIView commitAnimations];
  18. }

2、通过代码块使用UIKit动画

  1. -(void)animationOfBlock
  2. {
  3. //初始化一个View,用来显示动画
  4. UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
  5. redView.backgroundColor=[UIColor redColor];
  6. [self.view addSubview:redView];
  7. [UIView animateWithDuration:1 //时长
  8. delay:0 //延迟时间
  9. options:UIViewAnimationOptionTransitionFlipFromLeft//动画效果
  10. animations:^{
  11. //动画设置区域
  12. redView.backgroundColor=[UIColor blueColor];
  13. redView.frame=CGRectMake(50, 50, 200, 200);
  14. redView.alpha=0.5;
  15. } completion:^(BOOL finish){
  16. //动画结束时调用
  17. //............
  18. }];
  19. }

使用Core Animation对象来实现动画

在Core Animation中我们经常使用的是

  • CABasicAnimation
  • CAKeyframeAnimation
  • CATransitionAnimation

其中CABasicAnimationCAKeyframeAnimation是对图层中的不同属性进行动画的。

如果要多整个图层进行动画,则应该使用CATransitionAnimation

如果要使用组合动画,例如要改变图层的大小和透明度,则可以先为每个属性创建一个CABasicAnimation对象,再把他们组合到CAAnimationGroup中,最后把这个组合添加到要进行动画的CALayer中。

注:CAAnimation(以及CAAnimation的子类),全部都是显式动画,这样动画播放后,表现层回恢复到模型层的原始状态,这就意味着,如果动画播放完后,会恢复到原来的样子,所以在动画播放完后要对模型层进行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;

1、自定义动画:CABasicAnimation

  1. -(void)animationOfCABasicAnimation
  2. {
  3. //创建一个CABasicAnimation对象
  4. CABasicAnimation *animation=[CABasicAnimation animation];
  5. //设置颜色
  6. animation.toValue=(id)[UIColor blueColor].CGColor;
  7. //动画时间
  8. animation.duration=1;
  9. //是否反转变为原来的属性值
  10. animation.autoreverses=YES;
  11. //把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性
  12. [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
  13. }

2、关键帧动画:CAKeyframeAnimation

1. path

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

2. values

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

下面是改变依次改变view的颜色

  1. -(void)animationOfCAKeyframeAnimation
  2. {
  3. CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
  4. //设置属性值
  5. animation.values=[NSArray arrayWithObjects:
  6. (id)self.view.backgroundColor,
  7. (id)[UIColor yellowColor].CGColor,
  8. (id)[UIColor greenColor].CGColor,
  9. (id)[UIColor blueColor].CGColor,nil];
  10. animation.duration=3;
  11. animation.autoreverses=YES;
  12. //把关键帧添加到layer中
  13. [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
  14. }

3、使用路径制作动画:CAKeyframeAnimation

  1. -(void)animationOfCAKeyframeAnimationPath
  2. {
  3. //初始化一个View,用来显示动画
  4. UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
  5. redView.backgroundColor=[UIColor redColor];
  6. [self.view addSubview:redView];
  7. CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
  8. //初始化路径
  9. CGMutablePathRef aPath=CGPathCreateMutable();
  10. //动画起始点
  11. CGPathMoveToPoint(aPath, nil, 20, 20);
  12. CGPathAddCurveToPoint(aPath, nil,
  13. 160, 30,//控制点
  14. 220, 220,//控制点
  15. 240, 380);//控制点
  16. ani.path=aPath;
  17. ani.duration=10;
  18. //设置为渐出
  19. ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  20. //自动旋转方向
  21. ani.rotationMode=@"auto";
  22. [redView.layer addAnimation:ani forKey:@"position"];
  23. }

使用Core Animation对象来实现动画的更多相关文章

  1. iOS Core Animation学习总结(3)--动画的基本类型

    一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...

  2. core Animation之CAKeyframeAnimation(关键帧动画)

    CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...

  3. Core Animation中的组动画

    实际开发中一个物体的运动往往是复合运动,单一属性的运动情况比较少,但恰恰属性动画每次进行动画设置时一次只能设置一个属性进行动画控制(不管是 基础动画还是关键帧动画都是如此),这样一来要做一个复合运动的 ...

  4. Core Animation之CABasicAnimation(基础动画)

    #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UIButto ...

  5. Core Animation中的关键帧动画

    键帧动画就是在动画控制过程中开发者指定主要的动画状态,至于各个状态间动画如何进行则由系统自动运算补充(每两个关键帧之间系统形成的动画称为“补间动画”),这种动画的好处就是开发者不用逐个控制每个动画帧, ...

  6. Core Animation中的基础动画

    基础动画 在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UI ...

  7. 在ios中运用core animation暂停和继续动画

    本文转载至 http://blog.csdn.net/wildfireli/article/details/23191861 暂停和继续动画的核心代码如下: <pre name="co ...

  8. IOS动画(Core Animation)总结 (参考多方文章)

    一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...

  9. iOS 动画效果:Core Animation & Facebook's pop

    本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...

随机推荐

  1. JavaEE JavaBean 反射、内省、BeanUtils

    JavaEE JavaBean 反射.内省.BeanUtils @author ixenos JavaBean是什么 一种规范,表达实体和信息的规范,便于封装重用. 1.所有属性为private2.提 ...

  2. keepalived问题

    Sep 30 11:41:34 XSXSH-LB2 Keepalived[2735]: Starting Keepalived v1.2.12 (04/08,2014) Sep 30 11:41:34 ...

  3. Announcement

    本来是习惯把每天的内容写在一个txt里. 似乎不符合要求.无论格式还是内容.于是转战blog. 事实上.有专业课学习加上马上考四级以及下学期可能的专业调整.此学期时间紧张. 能完成日常作业并掌握周课内 ...

  4. A. Mike and Cellphone(Round 361 Div.2)

    写一半去开程序的时候不小心关了网页,LOFTER我都不奢望加入代码高亮,最起码我关闭的时候弹个对话框,再不济也给我定时保存一下草稿吧. A. Mike and Cellphone time limit ...

  5. nmon的安装与使用

    nmon的安装与使用 1.下载 nmon:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download nmonanalyser http://www. ...

  6. STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数

    1.为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟.转换后的电压值和I2C读出的数据. 2.通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化.之前用我以前公司SP0A19芯 ...

  7. CSS3秘笈:第十一章

    表格和表单的格式化 1.表格的各种标签提供了许多有用的“钩子”,可以再上面挂CSS样式.如果创建了<th>标签样式,那么每一个列的标题——<th>标签——看起来就有可能与其他的 ...

  8. VMware中Ubuntu 14.04出现Unknown Display问题解决

    如需转载请标明出处:http://blog.csdn.net/itas109 QQ技术交流群:129518033 今天安装完Ubuntu 14.04后,在虚拟机中显示不全,本来调节一下屏幕分辨率就可以 ...

  9. hdu_1072_Nightmare(BFS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题意:给你一个地图,让你在炸弹爆之前找到出口,最初炸弹设定为6,每走一格需要1,中途有地方能让炸 ...

  10. Linked List 实例

    文件功能:实现了动态建立一个学生信息的链表包括链表的创建.插入.删除.和打印输出学生信息包括姓名和分数 #include<iostream> #include<string> ...