iOS 幾種純色圓進(jìn)度條畫(huà)法


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];


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容