渐变颜色的进度条WGradientProgress-备用
今天我们来实现一个iOS平台上的进度条(progress bar or progress view)。这种进度条比APPLE自带的更加漂亮,更加有“B格”。它拥有渐变的颜色,而且这种颜色是动态移动的,这里称之为WGradientProgress。
先来看看我们的目标长什么样子:

WGradientProgress的使用方法很简单,主要有展示接口以及隐藏接口,目前显示的位置有两种选择:
WProgressPosDown //progress is on the down border of parent view,显示在parent view的底部(主流做法,默认)
- WProgressPosUp //progress is on the up border of parent view,也就是显示在parent view的顶部
主要的接口有以下几个:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
+ (WGradientProgress *)sharedInstance;/** * the main interface to show WGradientProgress obj, position is WProgressPosDown by default. * * @param parentView which view to be attach */- (void)showOnParent:(UIView *)parentView;/** * the main interface to show WGradientProgress obj * * @param parentView which view to be attach * @param pos up or down */- (void)showOnParent:(UIView *)parentView position:(WProgressPos)pos;/** * the main interface to hide WGradientProgress obj */- (void)hide; |
分析
这里我们看一下,实现出这样的效果需要解决哪些技术难点:
- 如何实现一个静态的具有渐变颜色的色带
- 如何实现色带颜色循环移动
- 如何关联进度值与色带的宽度
(1)如何实现一个静态的具有渐变颜色的色带
这里需要使用CALayer的子类CAGradientLayer。CAGradientLayer用于实现颜色渐变,关于CAGradietnLayer的介绍请看这里。我们使用到的属性有startPoint、endPoint、colors。
我们可以这样子做出一个静态的渐变色带,你也可以修改colors数组来实现不同颜色的色带:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
if (self.gradLayer == nil) { self.gradLayer = [CAGradientLayer layer]; self.gradLayer.frame = self.bounds;//尺寸要与view的layer一致}self.gradLayer.startPoint = CGPointMake(0, 0.5);self.gradLayer.endPoint = CGPointMake(1, 0.5);//create colors, important sectionNSMutableArray *colors = [NSMutableArray array];for (NSInteger deg = 0; deg <= 360; deg += 5) { UIColor *color; color = [UIColor colorWithHue:1.0 * deg / 360.0 saturation:1.0 brightness:1.0 alpha:1.0]; [colors addObject:(id)[color CGColor]];}[self.gradLayer setColors:[NSArray arrayWithArray:colors]]; |
(2)如何实现色带颜色循环移动
色带颜色循环向前移动,本质上是渐变图层gradientLayer的colors数组循环变化。如果理解了这点,那就很容易往下做了。我的做法是使用定时器NSTimer,让定时器的执行方法去循环地改变color数组。另外,既然要做到循环,那么应该循环地取colors数组的最后一个颜色值插到数组开始处。定时器的执行代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/** * here I use timer to circularly move colors */- (void)setupTimer{ CGFloat interval = 0.03; if (self.timer == nil) { self.timer = [NSTimer timerWithTimeInterval:interval target:self selector:@selector(timerFunc) userInfo:nil repeats:YES]; } [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];}/** * rearrange color array */- (void)timerFunc{ CAGradientLayer *gradLayer = self.gradLayer; NSMutableArray *copyArray = [NSMutableArray arrayWithArray:[gradLayer colors]]; UIColor *lastColor = [copyArray lastObject]; [copyArray removeLastObject]; if (lastColor) { [copyArray insertObject:lastColor atIndex:0]; } [self.gradLayer setColors:copyArray];} |
*强势插入:
NSTimer的启动、暂停、永远停止这三个操作要分清,尤其是暂停与停止:
- 启动:
|
1
2
3
4
5
6
|
- (void)startTimer{ //start timer [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; [self.timer setFireDate:[NSDate date]];} |
- 暂停:
|
1
2
3
4
5
6
7
8
|
/** * here we just pause timer, rather than stopping forever. * NOTE: [timer invalidate] is not fit here. */- (void)pauseTimer{ [self.timer setFireDate:[NSDate distantFuture]];} |
- 停止(无法再启动):
|
1
|
[self.timer invalidate] |
(3)如何关联进度值与色带的宽度
这个问题看起来很简单,但实际上隐藏着一个很好用的技术:mask。mask也称为蒙版,当我们给一个layer设置了mask layer后,layer就只显示出mask layer所覆盖到的区域,其他区域不显示。用伪代码可以描述为:
|
1
2
3
|
CALayer *layer = newlayer.mask = _maskLayer;layer.visualSection = _maskLayer.bounds; |
因此,我们可以将在一开始时就上文的渐变图层gradientLayer大小设置为与view同尺寸,然后通过mask layer设置可见区域。这样,进度条进度值设置问题就转化为mask layer的宽度问题了。
首先,我们添加一个mask layer到gradient layer上:
|
1
2
3
4
5
6
7
|
self.mask = [CALayer layer];[self.mask setFrame:CGRectMake(self.gradLayer.frame.origin.x, self.gradLayer.frame.origin.y, self.progress * self.width, self.height)];self.mask.borderColor = [[UIColor blueColor] CGColor];self.mask.borderWidth = 2;[self.gradLayer setMask:self.mask];[self.layer addSublayer:self.gradLayer]; |
然后相应进度值的改变如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
- (void)setProgress:(CGFloat)progress{ if (progress < 0) { progress = 0; } if (progress > 1) { progress = 1; } _progress = progress; CGFloat maskWidth = progress * self.width; self.mask.frame = CGRectMake(0, 0, maskWidth, self.height);} |
以上就是WGradientProgress的主要技术要点,更具体的细节以及使用方法请下载我github上的代码查看,下载时别忘记随手点个Star,给我更多支持与鼓励!
源代码下载:点我。https://github.com/weng1250/WGradientProgressDemo.git
渐变颜色的进度条WGradientProgress-备用的更多相关文章
- 【原】Github系列之三:开源iOS下 渐变颜色的进度条WGradientProgress
概述 今天我们来实现一个iOS平台上的进度条(progress bar or progress view).这种进度条比APPLE自带的更加漂亮,更加有“B格”.它拥有渐变的颜色,而且这种颜色是动态移 ...
- 纯css使用线性渐变实现滚动进度条(来自于微信前端早读课)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【iOS实现一个颜色渐变的弧形进度条】
在Github上看到一些进度条的功能,都是通过Core Graph来实现.无所谓正确与否,但是开发效率明显就差很多了,而且运行效率还是值得考究的.其实使用苹果提供的Core Animation能够非常 ...
- 超炫的HTML5粒子效果进度条 VS 如何规范而优雅地code
最近瞎逛的时候发现了一个超炫的粒子进度效果,有多炫呢?请擦亮眼镜! // _this.ch){ _this.particles.splice(i, 1); } }; this.Particle.p ...
- BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)
上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前 ...
- Bootstrap进度条
前面的话 在网页中,进度条的效果并不少见,比如一个评分系统,比如加载状态等,通过简单.灵活的进度条,可以为当前工作流程或动作提供实时反馈.本文将详细介绍Bootstrap进度条 基本样式 Bootst ...
- 不可思议的纯 CSS 滚动进度条效果
结论先行,如何使用 CSS 实现下述滚动条效果? 就是顶部黄色的滚动进度条,随着页面的滚动进度而变化长短. 在继续阅读下文之前,你可以先缓一缓.尝试思考一下上面的效果或者动手尝试一下,不借助 JS , ...
- Android多种样式的进度条
原创 2016年04月26日 16:46:35 标签: android / clip / 进度条 / 8473 编辑 删除 ---- The mark of the immature man is t ...
- Bootstrap各种进度条的实例讲解
本章将讲解 Bootstrap 进度条.在本教程中,您将看到如何使用bootstrap教程.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Interne ...
随机推荐
- C51程序优化
1.指针: 对于大部分的编译器,使用指针比使用数组生成的代码更短,执行效率更高.但是在Keil中则相反,使用数组比使用的指针生成的代码更短.通常使用自加.自减指令和复合赋值表达式(如a-=1及a+=1 ...
- Sum
Problem Description XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are ...
- Android中几种常用的话框
1.普通对话框: Builder alert=new AlertDialog.Builder(MainActivity.this); alert.setTitle("提示"); a ...
- HDU_2136——最大质因数,素数筛选法
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- [HNOI 2013] 旅行 (数学)
感觉此题难啊,数学还是太渣了,看了半天的题解才算明白了点儿. 题目大意 给一个长度为n且仅由1和-1组成的序列ai, i = 1, 2, ..., n,每个位置都有另一个值vi,要求用某种方案将序列划 ...
- tomcat端口占用后的解决办法
学 习网页设计的同学都会用到tomcat这个软件,在安装的时候我们一般都会选择端口为8080端口,这个端口一般情况下是不会有程序占用的,所以我们运行 tomcat不会出现什么问题,但是如果一旦别占用, ...
- ajax弹出窗口
提取自ZCMS的弹出框: 代替window.open.window.alert.window.confirm:提供良好的用户体验: 水晶质感,设计细腻,外观漂亮: 兼容ie6/7/8.firefox2 ...
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 【剑指offer】二叉搜索树的后序遍历序列
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26092725 剑指offer上的第24题,主要考察递归思想,九度OJ上AC. 题目描写叙述 ...
- uploadify3.1 参数 中文详解
langFile: 'http://www.static-xxx.nu/uploader/uploadifyLang_en.js',//语言包的路径,能设置所有的提示文字 swf: 'http://w ...