iOS 自定义控件 progressView(环形进度条)
2015-08-05 10:34
330 查看
转帖:http://blog.csdn.net/xiangzhang321/article/details/42688133
之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的。
先把自定义控件的效果图贴出来。
![](http://img.blog.csdn.net/20150113140653961?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNfb2Jq/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150113140635328?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNfb2Jq/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
其实我写的这个控件很简单。索性就直接把源码贴出来吧。
.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];
之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的。
先把自定义控件的效果图贴出来。
其实我写的这个控件很简单。索性就直接把源码贴出来吧。
.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 使用cocoapods遇见头文件找不到
- iOS-计算两个日期之间的天数
- iOS 对应用程序启动时所有方法的调用顺序分析(转自董铂然博客园)
- 苹果IOS开发者账号的区别,企业账号,个人账号,公司团队账号,教育账号
- ios禁止屏幕旋转
- iOS 7中的extendedLayout
- 利用dwarfdump 分析IOS crash
- iOS-基本知识汇总
- iOS 读书笔记-cookie
- iOS在照片上添加水印
- iOS 开发的中英文入门推荐书籍都有哪些?
- IOS Xcode 插件
- iOS图形库CorePlot
- xcode语法高亮插件
- iOS7使用iOS8上的方法报错处理
- iOS开发中的测试框架
- iOS开发_常用的正则表达式
- iOS开发基础之用三种不同方式创建Table View应用
- iOS项目的完整重命名方法图文教程
- iOS证书与推送通知证书制作过程遇到的问题