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

#1. 分析动画构成

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

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

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

我们分析下这张图的构成

#1. 一个灰色的背景

#2. 一个白色的圆环

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

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

灰色的背景,

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

白色的圆环,

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

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

闭合的圆弧,

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

//先画内圆

    //设置线条
path.lineWidth = ;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound; //设置圆心
[path moveToPoint:CGPointMake(_myWidth / , _myHeight / )]; //设置内切圆弧
[path addArcWithCenter:CGPointMake(_myWidth / , _myHeight / ) radius: startAngle: M_PI * / endAngle:M_PI * / + * M_PI / * _count clockwise:YES]; //线路闭合
[path closePath]; [path fill];

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

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

上代码:

先创建一个UIView子类,

#import <UIKit/UIKit.h>

@interface MyView : UIView

@property (nonatomic, assign) CGFloat   myWidth;
@property (nonatomic, assign) CGFloat myHeight;
@property (nonatomic, assign) NSInteger count; @end

在drawRect中添加图案

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame {

    CGRect myFrame = CGRectMake(frame.origin.x, , frame.size.width, frame.size.height);

    self = [super initWithFrame:myFrame];
if (self) { self.myWidth = frame.size.width;
self.myHeight = frame.size.height; } return self;
} - (void)drawRect:(CGRect)rect {
// Drawing code //设置线条颜色
UIColor *color = [UIColor whiteColor];
[color set]; //画内圆
UIBezierPath *path = [UIBezierPath bezierPath]; //设置线条
path.lineWidth = ;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound; //设置圆心
[path moveToPoint:CGPointMake(_myWidth / , _myHeight / )]; //设置内切圆弧
[path addArcWithCenter:CGPointMake(_myWidth / , _myHeight / )
radius: startAngle: M_PI * /
endAngle:M_PI * / + * M_PI / * _count
clockwise:YES]; //线路闭合
[path closePath]; [path fill]; //添加外圆
UIBezierPath *apath = [UIBezierPath bezierPath];
apath.lineWidth = ;
[apath addArcWithCenter:CGPointMake(, )
radius:
startAngle:
endAngle: * M_PI
clockwise:YES]; [apath stroke]; } @end

在Controller中调用

#import "ViewController.h"
#import "MyView.h" @interface ViewController () @property (nonatomic, strong) MyView *myView;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) NSInteger count; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建计时器
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(countOn)];
_displayLink.paused = YES; [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; //创建自定义View实例
_myView = [[MyView alloc] initWithFrame:CGRectMake(, , , )];
_myView.backgroundColor = [UIColor grayColor];
_myView.count = ;
[self.view addSubview:_myView]; //在这里可以添加额外动画, 设置hud出现的方式
[UIView animateWithDuration:0.5 animations:^{ _myView.frame = CGRectMake(, , , );
_displayLink.paused = NO;
}]; } //计时器事件, 修改动画参数
- (void)countOn { _count++;
_myView.count = _count; if (_count > ) { [_displayLink invalidate];
[_myView removeFromSuperview];
} //重绘
[_myView setNeedsDisplay];
}

创建简单动画(一) --- 常规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. doT模板

    框架源码地址 https://github.com/olado/doT <div id="main"> <script id="banner-templ ...

  2. 树莓派+Android Things

    在开始之前 谷歌前不久发布了Android Things面向物联网的系统,用意是想让android开发者用原来开发app的方式开发硬件相关的应用,扩展了android开发的方向和前景,而谷歌的Andr ...

  3. 通过HttpModule管道,帮助api对接开发

    我们公司的技术以.net为主,最近公司的项目需要和其它以java为主的公司搞对接.   .net提供webapi由java请求调用. 目前出现java说调用了,但是.net一直接收不到数据.两方开发人 ...

  4. ELK配置说明及个人理解

    ELK是开源的日志查询系统,由三个开源工具组成:Elasticsearch.Logstash和Kibana; Elasticsearch的功能主要用于进行日志数据的存储及查询. Logstash提供日 ...

  5. js--时钟(图片)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. Sun jdk, Openjdk, Icedtea jdk关系

    转自: http://blog.chinaunix.net/uid-20648944-id-3204527.html Sun jdk与Openjdk版本发展历史如下图所示: 1.    Openjdk ...

  7. 《高性能Javascript》读书笔记-4

    第四章 算法和流程控制 代码组织结构和解决具体问题的思路是影响代码性能的主要因素 循环处理是最常见的编程模式之一,也是提高性能的关注点之一 循环分四种:标准for循环 ; i < Things. ...

  8. 背包类问题解答——poj3624分析

    习题网址:http://poj.org/problem?id=3624 试题分析:该类题通过限定物品总数量.总质量:并且初始化每个物品的起始质量和一个量化的性质.最后求解最值的量化性质的值是多少的问题 ...

  9. 二、ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)

    阅读目录: 1.开篇介绍 2.ASP.NETMVC Controller 控制器的入口(Controller的执行流程) 3.ASP.NETMVC Controller 控制器的入口(Controll ...

  10. [POJ] String Matching

    String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 D ...