先说下当前我为处理动画的思路: (新手上路, 老司机轻喷,如果有更好的实现方法请大神指教 感恩戴德)

#1. 分析动画构成

#2. 如果是位移动画则考虑使用BasicAnimation或者KeyframeAnimation实现, 需要的话再搭配缓动函数

#3. 比较复杂的动画则考虑是否用UIBezierpath一帧帧来画

今天我们模仿做一个场景切换加载等待动画, 比如这样的

我们分析下这张图的构成

#1. 一个灰色的背景

#2. 一个白色的圆环

#3. 一个闭合的圆弧(白色部分)

看起来不是简单的位移动画了, 我们用UIBezierPath加CADisplayLink一帧一帧来画试试看

灰色的背景,

这个比较简单, 我们直接创建一个UIView子类, 背景颜色设置为灰色

白色的圆环,

可以用UIBezierPath直接画一个圆,注意调整线的宽度 So easy

  1. //添加外圆
  2. UIBezierPath *apath = [UIBezierPath bezierPath];
  3. apath.lineWidth = ;
  4. [apath addArcWithCenter:CGPointMake(, ) radius: startAngle: endAngle: * M_PI clockwise:YES];
  5.  
  6. [apath stroke];

闭合的圆弧,

一样用UIBezierPath, 先设置圆心 画一个圆弧然后闭合路径, _count是设置的一个变量, 有Controller中的计时器控制以达到动画的效果

  1. //先画内圆
  2.  
  3. //设置线条
  4. path.lineWidth = ;
  5. path.lineCapStyle = kCGLineCapRound;
  6. path.lineJoinStyle = kCGLineJoinRound;
  7.  
  8. //设置圆心
  9. [path moveToPoint:CGPointMake(_myWidth / , _myHeight / )];
  10.  
  11. //设置内切圆弧
  12. [path addArcWithCenter:CGPointMake(_myWidth / , _myHeight / ) radius: startAngle: M_PI * / endAngle:M_PI * / + * M_PI / * _count clockwise:YES];
  13.  
  14. //线路闭合
  15. [path closePath];
  16.  
  17. [path fill];

要注意调整外圆和内闭合弧的线宽

然后在Controller中创建计时器, 改变_count的值达到动画的效果

上代码:

先创建一个UIView子类,

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface MyView : UIView
  4.  
  5. @property (nonatomic, assign) CGFloat myWidth;
  6. @property (nonatomic, assign) CGFloat myHeight;
  7. @property (nonatomic, assign) NSInteger count;
  8.  
  9. @end

在drawRect中添加图案

  1. #import "MyView.h"
  2.  
  3. @implementation MyView
  4.  
  5. - (id)initWithFrame:(CGRect)frame {
  6.  
  7. CGRect myFrame = CGRectMake(frame.origin.x, , frame.size.width, frame.size.height);
  8.  
  9. self = [super initWithFrame:myFrame];
  10. if (self) {
  11.  
  12. self.myWidth = frame.size.width;
  13. self.myHeight = frame.size.height;
  14.  
  15. }
  16.  
  17. return self;
  18. }
  19.  
  20. - (void)drawRect:(CGRect)rect {
  21. // Drawing code
  22.  
  23. //设置线条颜色
  24. UIColor *color = [UIColor whiteColor];
  25. [color set];
  26.  
  27. //画内圆
  28. UIBezierPath *path = [UIBezierPath bezierPath];
  29.  
  30. //设置线条
  31. path.lineWidth = ;
  32. path.lineCapStyle = kCGLineCapRound;
  33. path.lineJoinStyle = kCGLineJoinRound;
  34.  
  35. //设置圆心
  36. [path moveToPoint:CGPointMake(_myWidth / , _myHeight / )];
  37.  
  38. //设置内切圆弧
  39. [path addArcWithCenter:CGPointMake(_myWidth / , _myHeight / )
  40. radius: startAngle: M_PI * /
  41. endAngle:M_PI * / + * M_PI / * _count
  42. clockwise:YES];
  43.  
  44. //线路闭合
  45. [path closePath];
  46.  
  47. [path fill];
  48.  
  49. //添加外圆
  50. UIBezierPath *apath = [UIBezierPath bezierPath];
  51. apath.lineWidth = ;
  52. [apath addArcWithCenter:CGPointMake(, )
  53. radius:
  54. startAngle:
  55. endAngle: * M_PI
  56. clockwise:YES];
  57.  
  58. [apath stroke];
  59.  
  60. }
  61.  
  62. @end

在Controller中调用

  1. #import "ViewController.h"
  2. #import "MyView.h"
  3.  
  4. @interface ViewController ()
  5.  
  6. @property (nonatomic, strong) MyView *myView;
  7. @property (nonatomic, strong) CADisplayLink *displayLink;
  8. @property (nonatomic, assign) NSInteger count;
  9.  
  10. @end
  11.  
  12. @implementation ViewController
  13.  
  14. - (void)viewDidLoad {
  15.  
  16. [super viewDidLoad];
  17.  
  18. //创建计时器
  19. _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(countOn)];
  20. _displayLink.paused = YES;
  21.  
  22. [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  23.  
  24. //创建自定义View实例
  25. _myView = [[MyView alloc] initWithFrame:CGRectMake(, , , )];
  26. _myView.backgroundColor = [UIColor grayColor];
  27. _myView.count = ;
  28. [self.view addSubview:_myView];
  29.  
  30. //在这里可以添加额外动画, 设置hud出现的方式
  31. [UIView animateWithDuration:0.5 animations:^{
  32.  
  33. _myView.frame = CGRectMake(, , , );
  34. _displayLink.paused = NO;
  35. }];
  36.  
  37. }
  38.  
  39. //计时器事件, 修改动画参数
  40. - (void)countOn {
  41.  
  42. _count++;
  43. _myView.count = _count;
  44.  
  45. if (_count > ) {
  46.  
  47. [_displayLink invalidate];
  48. [_myView removeFromSuperview];
  49. }
  50.  
  51. //重绘
  52. [_myView setNeedsDisplay];
  53. }

创建简单动画(一) --- 常规hud的更多相关文章

  1. Web Service 的创建简单编码、发布和部署

    最近,老大准备将已有的C/S架构项目中的通信部分做成通用,需要将其支持WebService为以后项目向着B/S架构升级做好铺垫,为此身为屌丝的我去各种百度WebService是个什么卵玩意,然后逐渐搭 ...

  2. UIView简单动画

    UIView动态实现的效果有以下几种: 1.动态改变frame 2.动态改变color 3.动态改变alpha 4.动态改变bounds 首先,我们先看几种BasicView动画 #pragma ma ...

  3. JQuery(二)——简单动画效果

    上一篇博客总结了JQuery的一些基本知识,这篇博客重点从JQuery能够制造各种各样的网页效果方面来进行总结.总结一些常见的常用的基本效果的必备方法.从隐藏显示,淡入淡出,滑动,动画等几个方面来简单 ...

  4. UI设计篇·入门篇·简单动画的实现,透明动画/旋转动画/移动动画/缩放动画,混合动画效果的实现,为动画设置监听事件,自定义动画的方法

    基本的动画构成共有四种:透明动画/旋转动画/移动动画/缩放动画. 配置动画的方式有两种,一种是直接使用代码来配置动画效果,另一种是使用xml文档配置动画效果 相比而言,用xml文档写出来的动画效果,写 ...

  5. Android成长之路-实现简单动画

    实现简单动画: 在drawable目录中放入图片, 并且创建xml文件 frame.xml 存入图片,如下: <pre class="html" name="cod ...

  6. iOS CAReplicatorLayer 简单动画

    代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...

  7. [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具

    ==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...

  8. Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机

    <Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global.如果是国内由世纪互联运维的Azure China,请参考这篇文档: Azure ...

  9. myeclipse(2015)中创建简单的Maven项目的步骤(用于生成可执行jar文件)------》myeclipse2015

    利用MyEclipse的引导,可以很方便的创建简单的.用于生成可执行jar文件的Maven项目: 1.New -> Project... 选择 Maven Project, 点击Next > ...

随机推荐

  1. cetos6 安装samba共享文件夹

    yum方式安装 yum install samba 修改配置文件 vim /etc/samba/smb.conf [global] comment = global workgroup = QFpay ...

  2. iOS项目导航栏返回按钮

    最近iOS项目中要求导航栏的返回按钮只保留那个箭头,去掉后边的文字,在网上查了一些资料,最简单且没有副作用的方法就是 [[UIBarButtonItem appearance] setBackButt ...

  3. Majority Element in an Array

    Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...

  4. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  5. linux提示语言包

    locale -a 查看可用的语言包.选择中文语言包 echo 'LC_ALL="zh_CN.utf8"' >> /etc/profileecho 'export LC ...

  6. Ajax的基本请求/响应模型

    一.Ajax工作核心 Ajax的核心是JavaScript对象XMLHttpRequest(简称XHR).它是一种支持异步请求的技术.可以通过使用XHR对象向服务器提出请求并处理响应,而不阻塞用户. ...

  7. Intent的几种Flag的不同

    冬天有点冷,不想写博客. 研究下Intent的几种Flag的不同: 1,FLAG_ACTIVITY_CLEAR_TOP:会清理掉目标activity栈上面所有的activity Intent inte ...

  8. 弹性布局EM的计算方法

    文章来源: http://www.w3cplus.com/css/px-to-em 总结: 1.浏览器默认的字体大小为16PX,即1em 2.EM可以指定小数点的后三位 3.元素自身没有设置字体大小, ...

  9. UVa 12100 Printer Queue (习题 5-7)

    传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), ...

  10. display: inline-block兼容性写法

    display:inline-block;*display:inline;*zoom:1;