创建简单动画(一) --- 常规hud
先说下当前我为处理动画的思路: (新手上路, 老司机轻喷,如果有更好的实现方法请大神指教 感恩戴德)
#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的更多相关文章
- Web Service 的创建简单编码、发布和部署
最近,老大准备将已有的C/S架构项目中的通信部分做成通用,需要将其支持WebService为以后项目向着B/S架构升级做好铺垫,为此身为屌丝的我去各种百度WebService是个什么卵玩意,然后逐渐搭 ...
- UIView简单动画
UIView动态实现的效果有以下几种: 1.动态改变frame 2.动态改变color 3.动态改变alpha 4.动态改变bounds 首先,我们先看几种BasicView动画 #pragma ma ...
- JQuery(二)——简单动画效果
上一篇博客总结了JQuery的一些基本知识,这篇博客重点从JQuery能够制造各种各样的网页效果方面来进行总结.总结一些常见的常用的基本效果的必备方法.从隐藏显示,淡入淡出,滑动,动画等几个方面来简单 ...
- UI设计篇·入门篇·简单动画的实现,透明动画/旋转动画/移动动画/缩放动画,混合动画效果的实现,为动画设置监听事件,自定义动画的方法
基本的动画构成共有四种:透明动画/旋转动画/移动动画/缩放动画. 配置动画的方式有两种,一种是直接使用代码来配置动画效果,另一种是使用xml文档配置动画效果 相比而言,用xml文档写出来的动画效果,写 ...
- Android成长之路-实现简单动画
实现简单动画: 在drawable目录中放入图片, 并且创建xml文件 frame.xml 存入图片,如下: <pre class="html" name="cod ...
- iOS CAReplicatorLayer 简单动画
代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...
- [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具
==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...
- Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
<Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global.如果是国内由世纪互联运维的Azure China,请参考这篇文档: Azure ...
- myeclipse(2015)中创建简单的Maven项目的步骤(用于生成可执行jar文件)------》myeclipse2015
利用MyEclipse的引导,可以很方便的创建简单的.用于生成可执行jar文件的Maven项目: 1.New -> Project... 选择 Maven Project, 点击Next > ...
随机推荐
- cetos6 安装samba共享文件夹
yum方式安装 yum install samba 修改配置文件 vim /etc/samba/smb.conf [global] comment = global workgroup = QFpay ...
- iOS项目导航栏返回按钮
最近iOS项目中要求导航栏的返回按钮只保留那个箭头,去掉后边的文字,在网上查了一些资料,最简单且没有副作用的方法就是 [[UIBarButtonItem appearance] setBackButt ...
- Majority Element in an Array
Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- linux提示语言包
locale -a 查看可用的语言包.选择中文语言包 echo 'LC_ALL="zh_CN.utf8"' >> /etc/profileecho 'export LC ...
- Ajax的基本请求/响应模型
一.Ajax工作核心 Ajax的核心是JavaScript对象XMLHttpRequest(简称XHR).它是一种支持异步请求的技术.可以通过使用XHR对象向服务器提出请求并处理响应,而不阻塞用户. ...
- Intent的几种Flag的不同
冬天有点冷,不想写博客. 研究下Intent的几种Flag的不同: 1,FLAG_ACTIVITY_CLEAR_TOP:会清理掉目标activity栈上面所有的activity Intent inte ...
- 弹性布局EM的计算方法
文章来源: http://www.w3cplus.com/css/px-to-em 总结: 1.浏览器默认的字体大小为16PX,即1em 2.EM可以指定小数点的后三位 3.元素自身没有设置字体大小, ...
- UVa 12100 Printer Queue (习题 5-7)
传送门:https://uva.onlinejudge.org/external/121/12100.pdf 题意:队列中待打印的任务(1 <= n <= 100)带有优先级(1-9), ...
- display: inline-block兼容性写法
display:inline-block;*display:inline;*zoom:1;