做iOS开发也有很长一段时间了,在这段时间里也一直没有什么写过什么东西,更多的是在看别人的博客,学习东西,今天闲来无事(其实更多的是不想看东西了),于是决定自己写一篇文章,一直觉得简书的界面很好看,而且平常更多的是在简书上看博客,便决定在这上面写自己的文章,记录我在开发上遇到的问题及解决方案,如果有什么不足之处还希望各位指正,废话不多说,下面开始正文。
这次要说的是绘制圆形进度条,最近再做一个类似网盘的功能,需要对网盘的文件进行上传、下载功能,在上传或者下载的过程中要显示文件的进度,当文件上传或者下载完成的时候,这个进度要从上传或者下载列表里面移除,因为对绘图一直内什么研究(简单的绘图比如画个三角形啊,矩形啊什么的还是会的),可是因为这个进度条涉及到未完成颜色,完成颜色什么的,当时也没有什么思路,于是自己研究了一下绘图,又下载了几个DEMO,结合了一下人家的代码,终于现了需求,话不多少,上代码
#import "WangPanCircleProgressView.h"
@implementation WangPanCircleProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
_percent = 0;
_width = 2;
}
return self;
}
- (void)setPercent:(float)percent{
_percent = percent;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[self addCircleBackgroundColor];
[self drawArc];
[self addCenterBack];
[self addCenterLabel];
}
//设置圆的背景色及画圆
-(void)addCircleBackgroundColor
{
CGColorRef color = (_circleBackgroundColor == nil) ? [UIColor grayColor].CGColor : _circleBackgroundColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width/2, viewSize.height/2);
//画圆
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 = (_circleUnFinishColr == nil) ? [UIColor redColor].CGColor : _circleUnFinishColr.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width/2, viewSize.height/2);
//画圆弧
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 = (M_PI * 2) * _percent - M_PI_2;
CGColorRef color = (_circleUnFinishColr == nil) ? [UIColor redColor].CGColor : _circleUnFinishColr.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width/2, viewSize.height/2);
//画圆弧
CGFloat radius = viewSize.width/2;
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, center.x, center.y);
CGContextAddArc(contextRef, center.x, center.y, radius, -0.5*M_PI, endAngle, 0);
CGContextSetFillColorWithColor(contextRef, color);
CGContextFillPath(contextRef);
}
}
//画中心的圆
-(void)addCenterBack
{
float width = (_width == 0) ? 5 : _width;
CGColorRef color = (_centerColor == nil)?[UIColor whiteColor].CGColor : _centerColor.CGColor;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGSize viewSize = self.bounds.size;
CGPoint center = CGPointMake(viewSize.width/2, viewSize.height/2);
//画圆弧
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 = 9 ;
UIColor *centerLabelColor = [UIColor grayColor];
if(_percent == 1)
{
percent = @"100%";
fontSize = 8;
centerLabelColor = (_centerColor == nil)?[UIColor grayColor] : _circleUnFinishColr;
[self performSelector:@selector(hiddenView) withObject:nil afterDelay:0.5];
}
else if (_percent<1 && _percent >= 0)
{
centerLabelColor = (_centerColor == nil)?[UIColor grayColor] : _circleUnFinishColr;
percent = [NSString stringWithFormat:@"%.0f%%",_percent*100];
}
CGSize viewSize = self.bounds.size;
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
paragraph.alignment = NSTextAlignmentCenter;
paragraph.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize],NSFontAttributeName ,centerLabelColor,NSForegroundColorAttributeName,[UIColor clearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];
[percent drawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize) withAttributes:attributes];
}
-(void)hiddenView
{
self.hidden = YES;
}
@end
在代码里当最后圆环进度为1即100%的时候让它隐藏了,这是我这方面的需求,如果不需要可以将方法注释,我觉的这点代码基本上已经可以满足大部分需求了,如果有不合适的地方,各位可以自己加以修改,第一次写,有什么不好的地方还望各位见谅