为什么80%的码农都做不了架构师?>>>   

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

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

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

[plain] view plaincopy

  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动画

[plain] view plaincopy

  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

[plain] view plaincopy

  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的颜色

[plain] view plaincopy

  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

[plain] view plaincopy

  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. }

转载于:https://my.oschina.net/aprill/blog/652586

CoreAnimation动画相关推荐

  1. GIF动画,菊花动画,UIView动画,CoreAnimation动画(CALayer动画)的用法

    1.GIF动画 1 // 创建一个显示图片的imageView // viewController创建 2 UIImageView *showGifImageView = [[UIImageView ...

  2. CoreAnimation动画(CALayer动画)

    1 #pragma mark - CABasicAnimation动画 2 - (IBAction)basicAnimation:(UIButton *)sender { 3 4 // 1.创建动画对 ...

  3. iOS 动画之CoreAnimation(CALayer)

    CoreAnimation基本介绍 CoreAnimation动画位于iOS框架的Media层 CoreAnimation动画实现需要添加QuartzCore.Framework CoreAnimat ...

  4. CoreAnimation (CALayer 动画)

    CoreAnimation基本介绍: CoreAnimation动画位于iOS框架的Media层 CoreAnimation动画实现需要添加QuartzCore.Framework CoreAnima ...

  5. 【动画2】CALayer动画

    一)UIView动画 二)CoreAnimation动画 前言:上一篇已经介绍了UIKit给我们封装好的UIView动画的使用,UIKit动画是建立在CoreAnimation动画之上的,CoreAn ...

  6. 关于iOS里的做动画方法的差别与注意事项

    CoreAnimation与UIView.animation... 这两个方式的主要差别在于,前者如果不主动设置,那么在动画做完以后,会恢复原状. 后者则不会,动画做完后是什么样,控件就是什么样. U ...

  7. iOS之深入探究动画渲染降帧

    一.为什么要对动画降帧? 众所周知,刷新频率越高体验越好,对于 iOS app 的刷新频率应该是越接近越 60fps 越好,主动给动画降帧,肯定会影响动画的体验.但是另一方面,我们也知道动画渲染的过程 ...

  8. ios动画原理 modelLayer和presentationLayer以及点击交互

    ios动画原理 modelLayer和presentationLayer以及点击交互 我们知道,iOS的动画,和其对应的layer有关. 之前在开发的过程中碰到一个问题,那就是,在一个视图的动画过程中 ...

  9. 从案例出发,由浅到深了解 iOS 动画

    收录:原文地址 前言 iOS 的动画框架很成熟,提供必要的信息,譬如动画的起始位置与终止位置,动画效果就出来了 动画的实现方式挺多的, 有系统提供的简单 API ,直接提供动画般的交互效果. 有手动设 ...

最新文章

  1. 生日QQ配对【找到你生日QQ了吗?】
  2. jquery总结06-动画事件03-淡入淡出效果
  3. Python OpenCV实例:图像直方图均衡化(数学公式简单实现)
  4. 威宝推出支持四接口的桌面硬盘新品
  5. dpkg: 錯誤: 分析檔案 '/var/lib/dpkg/updates/0001' 的第 0 行附近: 欄位名稱 `#padding' 中有換行
  6. 【HDU - 1870】愚人节的礼物(水题模拟 思想类似于栈?)
  7. Pmwiki基本编辑功能
  8. vue:在router里面给页面加title
  9. Bootstrap 容器(container)
  10. 爬取京东评论、分词+词频统计、词云图展示
  11. 两列自适应布局方案整理
  12. python结构模式_Python程序中的观察者模式结构编写
  13. rocketmq生产者发送到哪个队列
  14. C语言 扫雷游戏(代码+注释)
  15. linux安装键盘鼠标失灵,在archlinux安装界面这卡住了,鼠标键盘失灵
  16. 2021毛概知识点章节整理(完整版)
  17. 京东云云主机试用体验
  18. editplus里python技巧_editplus调试python
  19. 袁国勇、裴伟士、张杰和施敏四位科学家获2021未来科学大奖
  20. csp试题2:小明种苹果(绪)

热门文章

  1. 预测性智能的力量:AI 和机器学习将如何改变美国政府决策?
  2. 那些对程序员来说伤害性不高但是侮辱性极强的瞬间…
  3. RESTful Levels HATEOAS
  4. git查看各个branch之间的关系图
  5. 格式化的盘要怎么寻回文件
  6. jquery检验身份证规则
  7. c语言遍历字符串数组的方法
  8. Mrtg搭建流量监控服务器
  9. shell--特殊位置参数变量及常用内置变量
  10. How to use nheqminer in RedHat based systems (CentOS/Fedora)