iOS 简单实用的圆形进度条
来源:互联网 发布:分析英语句子成分软件 编辑:程序博客网 时间:2024/05/20 01:38
之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的。
先把自定义控件的效果图贴出来。
其实我写的这个控件很简单。索性就直接把源码贴出来吧。
// 新建一个View
.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];
- iOS 简单实用的圆形进度条
- 简单的圆形的进度条
- 简单的进度条,圆形进度条(一)
- iOS开发之简单实现圆形进度条
- HTML5 canvas基础---简单的圆形进度条
- 简单的自定义圆形进度条ProgressBar
- android 圆形进度条的简单实现
- 简单好看的圆形进度条对话框开源库
- 最屌最简单的圆形跟矩形进度条
- ios:圆形进度条1
- ios 圆形进度条
- iOS开发圆形进度条
- jQuery简单实用的轻量级进度条插件
- 自定义的圆形进度条
- iOS 图片加载 圆形进度条
- iOS 图片加载 圆形进度条
- iOS 图片加载 圆形进度条
- iOS封装圆形进度条控件
- 五子棋危险判断 - 图与连线
- 2016-4-26 linux 分布式框架以及相关模型
- 第14章css样式设置小技巧
- iOS 静态库
- yii2使用 db log
- iOS 简单实用的圆形进度条
- tomcat 设置启动参数(linux 和 windows)
- MVC中的KendoGrid里的一种传值方式
- start.S进一步、更详细的、深入的解释和分析 2013.04.26更新(五)
- ngrok原理浅析
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- MT4客户端通讯分析(二)——数据包解密
- Anemometer和pt-query-digest结合将MySQL慢查询可视化
- html仿今日头条数据列表