Demo:https://github.com/haozhema/loopprogressdemo/tree/master
Code in the Viewcontroller
#import "ViewController.h" #import "ProgressView.h" @interface Viewcontroller () @property (Strong, nonatomic) UISlider * Progressslider; @property (Strong, nonatomic) Progressview *progressview; @end @implementation viewcontroller-(void) viewdidload { [super viewdidload]; _progressview = [[Progressview alloc] Initwithframe:cgrectmake ([[UIScreen mainscreen] BOUNDS].SIZE.WIDTH/2-80, 100, )]; [Self.view Addsubview:_progressview]; _progressslider = [[UISlider alloc] Initwithframe:cgrectmake (self.view.frame.size.width/2-80, [+],]); [_progressslider addtarget:self Action: @selector (Sliderchange) forcontrolevents:uicontroleventvaluechanged]; _progressslider.maximumvalue = 1.0; _progressslider.minimumvalue = 0.0; [Self.view Addsubview:_progressslider];} -(void) sliderchange{ _progressview.percentage = _progressslider.value;} @end
Code in the Progressview
. H#import <UIKit/UIKit.h> @interface Progressview:uiview@property (Assign, nonatomic) cgfloat percentage; @end. m #import "ProgressView.h" @interface Progressview () @property (strong,nonatomic) UILabel *label; @end @implementation progressview-(Instancetype) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {self.percentage = 0.0; Self.backgroundcolor = [Uicolor Whitecolor]; [Self loadsubviews]; } return self;} -(void) loadsubviews{_label = [[UILabel alloc] Initwithframe:cgrectmake (Ten, SELF.FRAME.SIZE.HEIGHT/2-ten, SELF.FRAME.S IZE.WIDTH-20, 20)]; _label.textalignment = Nstextalignmentcenter; _label.font = [Uifont systemfontofsize:12]; _label.textcolor = [Uicolor blackcolor]; [Self Addsubview:_label];} -(void) Setpercentage: (cgfloat) percentage{_percentage = percentage; _label.text = [NSString stringwithformat:@ "%.2f%%", _percentage*100]; [Self setneedsdisplay];} -(void) DrawRect: (cgrect) rect{cgcontextrefContextref = Uigraphicsgetcurrentcontext (); Cgsize viewsize = self.bounds.size; Cgpoint Center = cgpointmake (VIEWSIZE.WIDTH/2, VIEWSIZE.HEIGHT/2); CGFloat radius = VIEWSIZE.WIDTH/2; Cgcontextbeginpath (CONTEXTREF); Cgcontextmovetopoint (Contextref, center.x, CENTER.Y); Cgcontextaddarc (Contextref, center.x, center.y, radius,-m_pi_2, 2*m_pi*_percentage-m_pi_2, 0); Cgcontextsetfillcolorwithcolor (Contextref, [Uicolor Redcolor]. Cgcolor); Cgcontextfillpath (CONTEXTREF); Filled circle, borderless Cgcontextaddarc (Contextref, center.x, Center.y, radius-10, 0, 2*M_PI, 0); Add a circle Cgcontextsetfillcolorwithcolor (Contextref, [Uicolor colorwithred:220/255.0 green:220/255.0 blue:220/255.0 Alpha:1]. Cgcolor); Cgcontextclosepath (CONTEXTREF); Cgcontextdrawpath (Contextref, Kcgpathfill);//Draw Fill Cgcontextstrokepath (contextref);//Painting Path}
IOS Simple Loop progress bar