环形进度条


.h文件的内容就是一些声明

#import

@interfaceProgressView :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)floatpercent;

//圆环宽度

@property(assign,nonatomic)floatwidth;

@end

.m文件里就是具体实现了

#import"ProgressView.h"

@implementationProgressView

- (id)initWithFrame:(CGRect)frame{

self= [superinitWithFrame:frame];

if(self) {

self.backgroundColor=ClearColor;

_percent=0;

_width=0;

}

returnself;

}

- (void)setPercent:(float)percent{

_percent= percent;

[selfsetNeedsDisplay];

}

- (void)drawRect:(CGRect)rect{

[selfaddArcBackColor];

[selfdrawArc];

[selfaddCenterBack];

[selfaddCenterLabel];

}

- (void)addArcBackColor{

CGColorRefcolor = (_arcBackColor==nil) ? [UIColorlightGrayColor].CGColor:_arcBackColor.CGColor;

CGContextRefcontextRef =UIGraphicsGetCurrentContext();

CGSizeviewSize =self.bounds.size;

CGPointcenter =CGPointMake(viewSize.width/2, viewSize.height/2);

// Draw the slices.

CGFloatradius = 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) {

CGColorRefcolor = (_arcFinishColor==nil) ? [UIColorgreenColor].CGColor:_arcFinishColor.CGColor;

CGContextRefcontextRef =UIGraphicsGetCurrentContext();

CGSizeviewSize =self.bounds.size;

CGPointcenter =CGPointMake(viewSize.width/2, viewSize.height/2);

// Draw the slices.

CGFloatradius = 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{

floatendAngle =2*M_PI*_percent;

CGColorRefcolor = (_arcUnfinishColor==nil) ? [UIColorblueColor].CGColor:_arcUnfinishColor.CGColor;

CGContextRefcontextRef =UIGraphicsGetCurrentContext();

CGSizeviewSize =self.bounds.size;

CGPointcenter =CGPointMake(viewSize.width/2, viewSize.height/2);

// Draw the slices.

CGFloatradius = 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{

floatwidth = (_width==0) ?5:_width;

CGColorRefcolor = (_centerColor==nil) ? [UIColorwhiteColor].CGColor:_centerColor.CGColor;

CGContextRefcontextRef =UIGraphicsGetCurrentContext();

CGSizeviewSize =self.bounds.size;

CGPointcenter =CGPointMake(viewSize.width/2, viewSize.height/2);

// Draw the slices.

CGFloatradius = 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 =@"";

floatfontSize =14;

UIColor*arcColor = [UIColorblueColor];

if(_percent==1) {

percent =@"100%";

fontSize =14;

arcColor = (_arcFinishColor==nil) ? [UIColorgreenColor] :_arcFinishColor;

}elseif(_percent<1&&_percent>=0){

fontSize =13;

arcColor = (_arcUnfinishColor==nil) ? [UIColorblueColor] :_arcUnfinishColor;

percent = [NSStringstringWithFormat:@"%0.2f%%",_percent*100];

}

CGSizeviewSize =self.bounds.size;

NSMutableParagraphStyle*paragraph = [[NSMutableParagraphStylealloc]init];

paragraph.alignment=NSTextAlignmentCenter;

NSDictionary*attributes = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:fontSize],NSFontAttributeName,arcColor,NSForegroundColorAttributeName,[UIColorclearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];

[percentdrawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize)withAttributes:attributes];

}

@end

你可能感兴趣的:(环形进度条)