创建简单动画(一) --- 常规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 > ...
随机推荐
- matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)”
matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)” 解决办法:traindata ...
- i++与++i区别使用
中心:使用变量自增或者处减使用尽量使用++i与--i,而不要去使用i++与i-- ++i与--i可以做左值也可以做右值,而i++与i--只能做右值. 对于基础类型i++与++i结果相同,但是i++肯定 ...
- AppDelegate 里一个基本的跳转方法,用来在rootView崩溃的时候直接调试我自己的页面
将 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lau ...
- 各个Maven仓库镜像(包括国内)
各个Maven仓库镜像(包括国内) 衽孤魍墓 ゅ槭 众矿工唯唯诺诺我在旁哭笑不得原 宦蠃サ 骘猩池 粑涫汾滹 吧滔哌蹋 飑俗た 狃攵庾唾 想必是想挡住什么我想反正这笔筒也不是 翡蜮胼 娴左 ...
- 生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)
#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, ...
- UIBezierPath与CAShapeLayer结合画扇形
/*让半径等于期望半径的一半 lineWidth等于期望半径 就可以画圆*/ 可以看出layer的走势是从圆边的中间一半在圆外 一半在圆内 因此让半径等于期望半径的一半 lineWidth等于期望半径 ...
- 运算程序,计算玩判断,Y继续,重复计算,N结束
#include "stdio.h" void main() { /*定义变量,d1,d2:第一.二个数 fu:符号 p1:接收判断号Y/N p2:接收的p1赋给p1 */ int ...
- LogFactory缺包异常
抛出异常: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/loggin ...
- python学习第一天内容整理
.cnblogs_code { width: 500px } 一.python 的历史 (摘自百度百科,了解就ok) Python[1] (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn ...
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)
传送门:http://codeforces.com/contest/757 A题题意是给你一个字符串,让你在里面找到"Bulbasaur"这样的单词有多少个,字符串可以重排列.实际 ...