iOS 自定义控件 progressView(环形进度条)
转帖:http://blog.csdn.net/xiangzhang321/article/details/42688133
之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的。
先把自定义控件的效果图贴出来。
其实我写的这个控件很简单。索性就直接把源码贴出来吧。
.h文件的内容就是一些声明
#import <UIKit/UIKit.h>
@interface ProgressView : UIView
//中心颜色
@property (strong, nonatomic)UIColor *centerColor;
//圆环背景色
@property (strong, nonatomic)UIColor *arcBackColor;
//圆环色
@property (strong, nonatomic)UIColor *arcFinishColor;
@property (strong, nonatomic)UIColor *arcUnfinishColor;
//百分比数值(0-1)
@property (assign, nonatomic)float percent;
//圆环宽度
@property (assign, nonatomic)float width;
@end
.m文件里就是具体实现了
#import "ProgressView.h"
@implementation ProgressView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = ClearColor;
_percent = 0;
_width = 0;
}
return self;
}
- (void)setPercent:(float)percent{
_percent = percent;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
[self addArcBackColor];
[self drawArc];
[self addCenterBack];
[self addCenterLabel];
}
- (void)addArcBackColor{
CGColorRef color = (_arcBackColor == nil) ? [UIColorlightGrayColor].CGColor : _arcBackColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
// Draw the slices.
CGFloat radius = viewSize.width / 2;
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, center.x, center.y);
CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
CGContextSetFillColorWithColor(contextRef, color);
CGContextFillPath(contextRef);
}
- (void)drawArc{
if (_percent == 0 || _percent > 1) {
return;
}
if (_percent == 1) {
CGColorRef color = (_arcFinishColor == nil) ? [UIColorgreenColor].CGColor : _arcFinishColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
// Draw the slices.
CGFloat radius = viewSize.width / 2;
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, center.x, center.y);
CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
CGContextSetFillColorWithColor(contextRef, color);
CGContextFillPath(contextRef);
}else{
float endAngle = 2*M_PI*_percent;
CGColorRef color = (_arcUnfinishColor == nil) ? [UIColorblueColor].CGColor : _arcUnfinishColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
// Draw the slices.
CGFloat radius = viewSize.width / 2;
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, center.x, center.y);
CGContextAddArc(contextRef, center.x, center.y, radius,0,endAngle, 0);
CGContextSetFillColorWithColor(contextRef, color);
CGContextFillPath(contextRef);
}
}
-(void)addCenterBack{
float width = (_width == 0) ? 5 : _width;
CGColorRef color = (_centerColor == nil) ? [UIColorwhiteColor].CGColor : _centerColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);
// Draw the slices.
CGFloat radius = viewSize.width / 2 - width;
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, center.x, center.y);
CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI, 0);
CGContextSetFillColorWithColor(contextRef, color);
CGContextFillPath(contextRef);
}
- (void)addCenterLabel{
NSString *percent = @"";
float fontSize = 14;
UIColor *arcColor = [UIColor blueColor];
if (_percent == 1) {
percent = @"100%";
fontSize = 14;
arcColor = (_arcFinishColor == nil) ? [UIColorgreenColor] : _arcFinishColor;
}else if(_percent < 1 && _percent >= 0){
fontSize = 13;
arcColor = (_arcUnfinishColor == nil) ? [UIColorblueColor] : _arcUnfinishColor;
percent = [NSStringstringWithFormat:@"%0.2f%%",_percent*100];
}
CGSize viewSize = self.bounds.size;
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:fontSize],NSFontAttributeName,arcColor,NSForegroundColorAttributeName,[UIColorclearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];
[percent drawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize)withAttributes:attributes];
}
@end
ProgressView *progress = [[ProgressViewalloc]initWithFrame:CGRectMake(detil.width-65, 10, 60, 60)];
progress.arcFinishColor = COLOR_STRING(@"#75AB33");
progress.arcUnfinishColor = COLOR_STRING(@"#0D6FAE");
progress.arcBackColor = COLOR_STRING(@"#EAEAEA");
progress.percent = 1;
[detil addSubview:progress];
转载于:https://www.cnblogs.com/zixiadaxian/p/4704030.html
iOS 自定义控件 progressView(环形进度条)相关推荐
- iOS通过CAShapeLayer和UIBezierPath画环形进度条
UIBezierPath可以绘制矢量路径,而CAShapeLayer是Layer的子类,可以在屏幕进行绘制,本文主要思想是:CAShapeLayer按照UIBezierPath的矢量路径进行绘制. 效 ...
- WPF自定义控件(教程含源码)-圆形进度条、环形进度条
使用环形进度条显示用量百分比 控件效果如下 控件的关键属性如下: Background:控制背景圆环的原色. Stroke:控制进度圆环颜色.以及中间文本颜色. Value:进度百分比,double类 ...
- 自定义控件——环形进度条
系统提供的环形进度条无法显示进度,且不会停止转动,所以要用到能显示进度的进度条时,只能自己定义一个控件. API Demos里面有提供类似的画法,API Demos --> Graphics - ...
- iOS 一分钟学会环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低.只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先制作一个不带颜色渐变的进度条 ...
- [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 【WPF】环形进度条
WPF中自带有长条形的进度条,大部分场景都算适用,但是仍然有一部分空间小的场景不太合适,此时我们想到安卓上常用的环形进度条,美观,又不占空间. 那么WPF中的环形进度条控件在哪呢? 很遗憾,自带组件中 ...
- Qt编写自定义控件14-环形进度条
前言 环形进度条,用来展示当前进度,为了满足大屏UI的需要特意定制,以前有个叫圆环进度条,不能满足项目需要,只能重新定做,以前的进度间距不能自适应分辨率,而且当前进度对应的反的进度不能单独设置颜色,即 ...
- Android花样loading进度条(四)-渐变色环形进度条
背景 Android花样loading进度条系列文章主要讲解如何自定义所需的进度条,包括水平.圆形.环形.圆弧形.不规则形状等. 本篇我们对配文字环形进度条稍加变换,将圆环颜色改为渐变色的形式,使得进 ...
- Android花样loading进度条(二)-简单环形进度条
背景 Android花样loading进度条系列文章主要讲解如何自定义所需的进度条,包括水平.圆形.环形.圆弧形.不规则形状等. 本篇我们从圆形进度条讲起,讲简单形式的环形进度条,只有进度色彩,没有进 ...
最新文章
- 计算机三级偏硬汇编语言程序设计
- 迅为linux下串口,迅为IMX6ULL开发板Linux RS232/485驱动实验(上)
- 第一章 Java快速入门
- 【第一季】CH07_FPGA_RunLED创建VIVADO工程实验
- vue-router 路由跳转
- JAVAWEB入门之Sevlet的执行原理
- java node子节点_使用Java的XPath循环遍历节点并提取特定的子节点值
- 我爱你们,我的家人和朋友
- ssh2的application.xml配置文件配置详解
- 【Pycharm IDE】修改字体大小/设置缩略图/设置高亮
- appium相关记录
- linux内核C -- 第04课:Linux内核第一宏——container_of
- c# ASCII转换,数字转字母,字母转数字
- 接入广告App 教你如何赚取你的第一桶金 - 2048(含源码)
- C/C++语言——数据类型
- 京东到家程序员离职当天删库跑路
- 鸿蒙os商用版,鸿蒙OS再次官宣最快今年9月商用,可以一夜之间代替安卓系统
- python批量打印网页_web 批量打印
- 费诺编码的MATLAB递归实现
- 给定一个链表,判断链表中是否有环
热门文章
- python pca降维_机器学习之sklearn中的降维算法
- 基于java银行ATM管理系统设计(含源文件)
- 机器人码垛搬运编程程序_码垛机器人市场进一步扩张,解放人力搬运跑不了了...
- Wireshark文档阅读笔记-TCP 3 way handshaking解析与实例
- Qt文档阅读笔记-QWindow的进一步认识
- 浙江大学计算机科学与技术学院分数线,浙江大学录取分数线一般在多少 高考最低多少分能上浙大...
- Php循环函数嵌套javascript,JS循环嵌套问题?
- linux nfs mysql_MySQL实现高可用+共享存储NFS
- 【C语言重点难点】数据类型、常量和变量
- 2-4:套接字(Socket)编程之TCP通信