直接上代码了
如下
.h文件
@interface DrawingView : UIView
@property(nonatomic,assign)BOOL circle;
@property(nonatomic,strong)NSString* percent;
@end
#import "DrawingView.h"
#define PI 3.14159265358979323846
@implementation DrawingView
-(id)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame]){
}
return self;
}
-(void)drawRect:(CGRect)rect
{
if (!_circle) {
self.backgroundColor=[UIColor clearColor];
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGFloat lengths[] = {3,1};//此值可以确定所花虚线的点数
CGContextSetLineDash(context, 0, lengths,2);
CGContextSetLineDash(context, 0,lengths,2);
CGContextMoveToPoint(context, 0.0, 1.0);
CGContextAddLineToPoint(context, 300.0,1.0);
CGContextStrokePath(context);
}else
{
int selfPercent=(int)([self.percent floatValue]*100);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5);
CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-11)/2, 0, 2*PI, 0);
CGContextSetStrokeColorWithColor(context, [UtilityHelper colorWithHexString:@"#efefef"].CGColor);
CGContextDrawPath(context, kCGPathStroke);
CGContextSetLineWidth(context, 5);
CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-11)/2, -PI/2, (selfPercent*2*PI)/100-PI/2, 0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextDrawPath(context, kCGPathStroke);
UILabel *percentLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height)];
percentLabel.text=[NSString stringWithFormat:@"%d%%",(int)(selfPercent)];
percentLabel.backgroundColor=[UIColor clearColor];
percentLabel.textColor=[UIColor redColor];
if (selfPercent==0) {
percentLabel.textColor=[UtilityHelper colorWithHexString:@"#efefef"];
}
percentLabel.font=[UIFont systemFontOfSize:10];
percentLabel.textAlignment=NSTextAlignmentCenter;
[self addSubview:percentLabel];
/*
CGContextSetLineWidth(context, 0.5);
CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-15)/2, 0, 2*PI, 0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextDrawPath(context, kCGPathStroke);
CGContextSetLineWidth(context, 0.5);
CGContextAddArc(context, self.bounds.origin.x+(self.bounds.size.width)/2, self.bounds.origin.y+(self.bounds.size.height)/2, (self.bounds.size.width-5)/2, 0, 2*PI, 0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextDrawPath(context, kCGPathStroke);
*/
}
}
@end
在alloc这个view的时候一定要设置view的backgroundcolor=clearcolor 否则图画不出来