////CCProgressView.h//Demo////Created by Leao on 2017/8/7.//Copyright 2017 Zaodao. All rights reserved.//#import<UIKit/UIKit.h>typedef ns_enum (Nsinteger, Ccprogressviewstyle) {ccprogressviewstylecircle, //Round progress barCcprogressviewstylebar,//Bar Progress BarsCcprogressviewstyledefault =ccprogressviewstylecircle,};@interfaceCcprogressview:uiview@property (nonatomic, assign, setter=setprogress:) CGFloat progress;//0.0 ~ 1.0@property (nonatomic, assign) Ccprogressviewstyle Progressviewstyle; //progress bar Style@property (nonatomic, strong) Uicolor *tracktintcolor;//progress bar Background color@property (nonatomic, strong) Uicolor *progresstintcolor;//progress bar Color@property (nonatomic, strong) Uicolor *progressfulltintcolor;//Color of Progresstint when progress is complete@property (nonatomic, assign) CGFloat linewidth;//Draw Progress Width default:10@property (nonatomic, assign) CGFloat trackerwidth;//Draw Progress Width default:10//ccprogressviewstylecircle Effective@property (nonatomic, strong) Uicolor *fillcolor;//Center Color@property (nonatomic, assign) BOOL clockwise;//whether it is clockwise Default:yes@property (nonatomic, assign) CGFloat startangle;//progress bar starts angle, default:-m_pi/2.0@property (nonatomic, strong) UIButton *centerbtn;//label that records progress@property (nonatomic, strong) Uicolor *labelbackgroundcolor;//label's background color default Clearcolor@property (nonatomic, strong) Uicolor *textcolor;//label's font color default black@property (nonatomic, strong) Uifont *textfont;//Label font size defaults to- (void) Setprogress: (cgfloat) Progress;- (void) Setprogress: (cgfloat) Progress animated: (BOOL) animated;@end
////ccprogressview.m//Demo////Created by Leao on 2017/8/7.//Copyright 2017 Zaodao. All rights reserved.//#import "CCProgressView.h"#import<pop/POP.h>#import<objc/runtime.h>#import<ReactiveCocoa/ReactiveCocoa.h>#defineKccprogressfillcolor [Uicolor Clearcolor]#defineKccprogresstintcolor RGBCOLOR (214, 88, 45)#defineKcctracktintcolor RGBCOLOR (243, 212, 187)#defineProgress_width Self.frame.size.width#defineProgress_height Self.frame.size.height#defineKanimtimeinterval 2@interfaceCcprogressview () @property (nonatomic, strong) Cashapelayer*Tracklayer, @property (nonatomic, strong) Cashapelayer*Progresslayer;@end@implementationCcprogressview-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[self initsubviews]; } returnSelf ;}-(instancetype) init{ Self=[Super Init]; if(self) {[self initsubviews]; } returnSelf ;}#pragmamark-private-(void) initsubviews{_progressviewstyle=Ccprogressviewstyledefault; _progresstintcolor=Kccprogresstintcolor; _tracktintcolor=Kcctracktintcolor; _linewidth=Ten; _trackerwidth=Ten; _fillcolor=Kccprogressfillcolor; _clockwise=YES; _startangle=-M_PI/2.0; Self.backgroundcolor=[Uicolor Clearcolor]; Self.tracklayer=[Cashapelayer layer]; Self.trackLayer.lineCap=Kcalinecapbutt; Self.trackLayer.lineJoin=Kcalinecapbutt; Self.trackLayer.lineWidth=_linewidth; Self.trackLayer.fillColor=Nil; Self.trackLayer.strokeColor=_tracktintcolor.cgcolor; Self.trackLayer.frame=Self.bounds; [Self.layer AddSublayer:self.trackLayer]; Self.progresslayer=[Cashapelayer layer]; Self.progressLayer.lineCap=Kcalinecapbutt; Self.progressLayer.lineJoin=Kcalinecapbutt; Self.progressLayer.lineWidth=_trackerwidth; Self.progressLayer.fillColor=_fillcolor.cgcolor; Self.progressLayer.strokeColor=_progresstintcolor.cgcolor; Self.progressLayer.frame=Self.bounds; [Self.layer AddSublayer:self.progressLayer]; Self.progressLayer.strokeEnd=0.0; [Self addSubview:self.centerBtn];}- (void) layoutsubviews{[Super Layoutsubviews]; [Self Updatelayerpath];}#pragmamark-private-(UIButton *) centerbtn{if(!_centerbtn) {_centerbtn= [[UIButton alloc] Initwithframe:cgrectmake (0,0, Progress_width- -, Progress_height- -)]; _centerbtn.center= Cgpointmake (progress_width/2, progress_height/2); _centerbtn.titlelabel.textalignment=Nstextalignmentcenter; _centerbtn.layer.cornerradius= _centerbtn.width/2; _centerbtn.backgroundcolor= RGBCOLOR (246,227,204); _centerbtn.titlelabel.adjustsfontsizetofitwidth=YES; _centerbtn.userinteractionenabled=NO; _centerbtn.layer.maskstobounds=YES; } return_centerbtn;}- (void) updatelayerpath{if(_progressviewstyle = =ccprogressviewstylecircle) {Self.trackLayer.frame=Self.bounds; Self.progressLayer.frame=Self.bounds; CGFloat radius= Cgrectgetwidth (Self.frame) > Cgrectgetheight (self.frame)?(Cgrectgetheight (self.frame)-_linewidth)/2.0: (Cgrectgetwidth (Self.frame)-_linewidth)/2.0; Uibezierpath*bezierpath = [Uibezierpath bezierPathWithArcCenter:self.progressLayer.position radius:radius startangle:_ StartAngle endangle:_clockwise? _startangle +2* M_PI: _startangle-2*M_pi Clockwise:_clockwise]; Self.trackLayer.path=Bezierpath.cgpath; Self.progressLayer.path=Bezierpath.cgpath; } Else{self.trackLayer.frame= CGRectMake (0, (Cgrectgetheight (self.frame)-_linewidth)/2.0, Cgrectgetwidth (self.frame), _linewidth); Self.progressLayer.frame=Self.trackLayer.frame; Uibezierpath*bezierpath =[Uibezierpath Bezierpath]; [Bezierpath Movetopoint:cgpointmake (0, SELF.PROGRESSLAYER.POSITION.Y)]; [Bezierpath Addlinetopoint:cgpointmake (Cgrectgetwidth (self.frame), SELF.PROGRESSLAYER.POSITION.Y)]; Self.trackLayer.path=Bezierpath.cgpath; Self.progressLayer.path=Bezierpath.cgpath; }}#pragmamark-setter-(void) Settracktintcolor: (Uicolor *) tracktintcolor{_tracktintcolor=Tracktintcolor; Self.trackLayer.strokeColor=Tracktintcolor.cgcolor;}- (void) Setprogresstintcolor: (Uicolor *) progresstintcolor{_progresstintcolor=Progresstintcolor; Self.progressLayer.strokeColor=Progresstintcolor.cgcolor;}- (void) Setprogressfulltintcolor: (Uicolor *) progressfulltintcolor{_progressfulltintcolor=Progressfulltintcolor; if(Self.progressLayer.strokeEnd >=1.0) {self.progressLayer.strokeEnd=1.0; Self.progressLayer.strokeColor=_progressfulltintcolor.cgcolor; }}- (void) Setlinewidth: (cgfloat) linewidth{_linewidth=linewidth;//self.trackLayer.lineWidth = linewidth;Self.progressLayer.lineWidth =linewidth; if(_progressviewstyle! =ccprogressviewstylecircle) {[Self updatelayerpath]; }}- (void) Settrackerwidth: (cgfloat) trackerwidth {_trackerwidth=Trackerwidth; Self.trackLayer.lineWidth=_trackerwidth; if(_progressviewstyle! =ccprogressviewstylecircle) {[Self updatelayerpath]; }}#pragmaMark-setter (ccprogressviewstylecircle)-(void) Setfillcolor: (Uicolor *) fillcolor{_fillcolor=FillColor; Self.progressLayer.fillColor=Fillcolor.cgcolor;}- (void) Setclockwise: (BOOL) clockwise{_clockwise=clockwise; [Self Updatelayerpath];}- (void) Setstartangle: (cgfloat) startangle{_startangle=startangle; [Self Updatelayerpath];}- (void) Setprogress: (cgfloat) progress{[self setprogress:progress animated:no];}- (void) Setprogress: (cgfloat) Progress animated: (BOOL) animated{if(animated) {//The animations here can be directly used cabasicanimationPopbasicanimation *basicanim =[Popbasicanimation Animationwithpropertynamed:kpopshapelayerstrokeend]; if(Basicanim) {basicanim.duration=Kanimtimeinterval; Basicanim.tovalue=@ (progress); } Else{Basicanim=[Popbasicanimation Animationwithpropertynamed:kpopshapelayerstrokeend]; Basicanim.fromvalue=@ (SELF.PROGRESSLAYER.STROKEEND); Basicanim.tovalue=@ (progress); Basicanim.duration=4*Kanimtimeinterval; Basicanim.removedoncompletion=YES; } @weakify (self); Basicanim.completionblock= ^ (Popanimation *Anim, BOOL finished) {@strongify (self); Poppropertyanimation*basicanim = (Poppropertyanimation *) Anim; Self.progressLayer.strokeEnd=[Basicanim.tovalue Doublevalue]; if(Self.progressLayer.strokeEnd >=1.0&&_progressfulltintcolor) {Self.progressLayer.strokeEnd=1.0; Self.progressLayer.strokeColor=_progressfulltintcolor.cgcolor; } }; [Self.progresslayer Pop_addanimation:basicanim forkey:kpopshapelayerstrokeend]; } Else{self.progressLayer.strokeEnd=progress; if(Self.progressLayer.strokeEnd >=1.0&&_progressfulltintcolor) {Self.progressLayer.strokeEnd=1.0; Self.progressLayer.strokeColor=_progressfulltintcolor.cgcolor; } }}@end
IOS Round progress bar