圓形進度條的繪制

做iOS開發(fā)也有很長一段時間了,在這段時間里也一直沒有什么寫過什么東西,更多的是在看別人的博客,學習東西,今天閑來無事(其實更多的是不想看東西了),于是決定自己寫一篇文章,一直覺得簡書的界面很好看,而且平常更多的是在簡書上看博客,便決定在這上面寫自己的文章,記錄我在開發(fā)上遇到的問題及解決方案,如果有什么不足之處還希望各位指正,廢話不多說,下面開始正文。
這次要說的是繪制圓形進度條,最近再做一個類似網(wǎng)盤的功能,需要對網(wǎng)盤的文件進行上傳、下載功能,在上傳或者下載的過程中要顯示文件的進度,當文件上傳或者下載完成的時候,這個進度要從上傳或者下載列表里面移除,因為對繪圖一直內(nèi)什么研究(簡單的繪圖比如畫個三角形啊,矩形啊什么的還是會的),可是因為這個進度條涉及到未完成顏色,完成顏色什么的,當時也沒有什么思路,于是自己研究了一下繪圖,又下載了幾個DEMO,結合了一下人家的代碼,終于現(xiàn)了需求,話不多少,上代碼

屏幕快照 2016-05-31 20.38.34.png
#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

在代碼里當最后圓環(huán)進度為1即100%的時候讓它隱藏了,這是我這方面的需求,如果不需要可以將方法注釋,我覺的這點代碼基本上已經(jīng)可以滿足大部分需求了,如果有不合適的地方,各位可以自己加以修改,第一次寫,有什么不好的地方還望各位見諒

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容