IOS Simple Loop progress bar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.