1.繪圖
創(chuàng)建一個(gè)進(jìn)度條View在.h中聲明如下:
#import@interface CircleProgress : UIView
{
CGContextRef ctx;
}
@property (nonatomic, assign) float lineWidth;
@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, assign) float startAngle;
@property (nonatomic, assign) float endAngle;
@end
.m實(shí)現(xiàn)如下
- (void)drawRect:(CGRect)rect {
// Drawing code
if (_startAngle == 0)
{
_startAngle = 0;
}
if (_endAngle == 0)
{
_endAngle = 360.0f;
}
if (_lineWidth == 0)
{
_lineWidth = 3;
}
if (_lineColor == nil)
{
_lineColor = [UIColor blackColor];
}
ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
[_lineColor set];// 兩種設(shè)置顏色的方式都可以
CGContextSetLineWidth(ctx, _lineWidth); // 線的寬度
CGContextSetLineCap(ctx, kCGLineCapRound);
//圖形上下文暗甥,x,y為圓點(diǎn)坐標(biāo),半徑? startAngle為開(kāi)始的弧度救欧,endAngle為 結(jié)束的弧度,clockwise 0為順時(shí)針凛虽,1為逆時(shí)針
CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, self.frame.size.width/2 - _lineWidth , _startAngle / (180/M_PI),_endAngle /(180/M_PI), 0);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextStrokePath(ctx);
CGContextAddArc(ctx,self.frame.size.width/2, self.frame.size.height/2, 10, 0, 2 * M_PI, 1);
CGContextSetFillColorWithColor(ctx, _lineColor.CGColor);
CGContextFillPath(ctx);
}
-(void)setEndAngle:(float)endAngle
{
_endAngle = endAngle;
[self setNeedsDisplay];
}
-(void)setStartAngle:(float)startAngle
{
_startAngle = startAngle;
[self setNeedsDisplay];
}
-(void)setLineWidth:(float)lineWidth
{
_lineWidth = lineWidth ;
[self setNeedsDisplay];
}
-(void)setLineColor:(UIColor *)lineColor
{
_lineColor = lineColor;
[self setNeedsDisplay];
}
具體實(shí)現(xiàn)如下:
_circleProgressView = [[CircleProgress alloc] initWithFrame:CGRectMake(40, 30, 240, 240)];
_circleProgressView.backgroundColor = [UIColor redColor];
_circleProgressView.lineWidth = 25;
_circleProgressView.lineColor = [UIColor blackColor];
_circleProgressView.startAngle = 0;
_circleProgressView.endAngle = 360;
[self.view addSubview:_circleProgressView];
第一種方法完工本涕。
2.換湯不換藥的再來(lái)一種? 用UIBezierPath來(lái)實(shí)現(xiàn)
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref =? UIGraphicsGetCurrentContext();
CGContextBeginPath(ref);
[[UIColor blueColor] set];
CGContextSetLineWidth(ref, 10);
CGContextSetLineCap(ref, kCGLineCapRound);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 - 10 startAngle:0 endAngle:_endAngle/180.0 *M_PI clockwise:YES];
CGContextAddPath(ref, path.CGPath);
CGContextStrokePath(ref);
}
- (void)setEndAngle:(CGFloat)endAngle
{
_endAngle = endAngle;
[self setNeedsDisplay];
}
3.用layer來(lái)完成
用layer來(lái)完成相應(yīng)的純色進(jìn)度條比較方便快捷。
//創(chuàng)建CAShapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(0, 0, 200, 200);
//創(chuàng)建路徑
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.center radius:self.frame.size.width/2 - 10 startAngle:0 endAngle:M_PI clockwise:YES];
layer.path = path.CGPath;
layer.lineWidth = 9;
layer.fillColor=[UIColor clearColor].CGColor;
layer.strokeColor=[UIColor colorWithRed:0.76f green:0.89f blue:0.89f alpha:1.00f].CGColor;
layer.lineCap = kCALineCapRound;
layer.lineJoin = kCALineJoinRound;
[sel.view.layer addSublayer:layer];