iOS Draw Ring progress bar

Source: Internet
Author: User
<span id="Label3"></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span><UIKit/UIKit.h><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Sncircleprogressview:uiview</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * Progress value between 0-1.0</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@property (nonatomic,assign) cgfloat progressvalue;</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* Edge Width</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@property (nonatomic,assign) cgfloat progressstrokewidth;</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * progress bar Color</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@property (nonatomic,strong) Uicolor</span></span>*<span style="color: #000000;"><span style="color: #000000;">progresscolor;</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * progress bar track Color</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@property (nonatomic,strong) Uicolor</span></span>*<span style="color: #000000;"><span style="color: #000000;">progresstrackcolor;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">SNCircleProgressView.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Sncircleprogressview () {cashapelayer</span></span>*backgroundlayer;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Background Layer</span></span>Cashapelayer *frontfilllayer;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">layer to fill with</span></span>Uibezierpath *backgroundbezierpath;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Background Boussaire curve</span></span>Uibezierpath *frontfillbezierpath;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the Boussaire curve used to fill</span></span><span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Sncircleprogressview</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@synthesize</span></span>Progresscolor =<span style="color: #000000;"><span style="color: #000000;">_progresscolor;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@synthesize</span></span>Progresstrackcolor =<span style="color: #000000;"><span style="color: #000000;">_progresstrackcolor;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@synthesize</span></span>Progressvalue =<span style="color: #000000;"><span style="color: #000000;">_progressvalue;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@synthesize</span></span>Progressstrokewidth =<span style="color: #000000;"><span style="color: #000000;">_progressstrokewidth;</span></span>-(instancetype) initwithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) adecoder{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self =<span style="color: #000000;"><span style="color: #000000;">[super Initwithcoder:adecoder]) {[self setUp]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">;}</span></span>-<span style="color: #000000;"><span style="color: #000000;">(instancetype) initwithframe: (cgrect) frame{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self =<span style="color: #000000;"><span style="color: #000000;">[super Initwithframe:frame]) {[self setUp]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">;}</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * Initialize create layer</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) setup{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a background layer</span></span>Backgroundlayer =<span style="color: #000000;"><span style="color: #000000;">[cashapelayer layer]; Backgroundlayer.fillcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; Backgroundlayer.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.bounds; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a fill layer</span></span>Frontfilllayer =<span style="color: #000000;"><span style="color: #000000;">[cashapelayer layer]; Frontfilllayer.fillcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; Frontfilllayer.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.bounds; [self.layer addsublayer:backgroundlayer]; [self.layer addsublayer:frontfilllayer];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) setprogresscolor: (uicolor *<span style="color: #000000;"><span style="color: #000000;">) Progresscolor{_progresscolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">progresscolor; Frontfilllayer.strokecolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">progresscolor.cgcolor;}</span></span>-(uicolor *<span style="color: #000000;"><span style="color: #000000;">) progresscolor{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_progresscolor;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) setprogresstrackcolor: (uicolor *<span style="color: #000000;"><span style="color: #000000;">) Progresstrackcolor{_progresstrackcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">progresstrackcolor; Backgroundlayer.strokecolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">progresstrackcolor.cgcolor; Backgroundbezierpath</span></span>= [uibezierpath BezierPathWithArcCenter:self.center radius: (cgrectgetwidth (self.bounds)-self.progressstrokewidth) /<span style="color: #800080;"><span style="color: #800080;">2</span></span>. F startangle:<span style="color: #800080;"><span style="color: #800080;">0</span></span>endangle:m_pi*<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">clockwise:yes]; Backgroundlayer.path</span></span>=<span style="color: #000000;"><span style="color: #000000;">backgroundbezierpath.cgpath;}</span></span>-(uicolor *<span style="color: #000000;"><span style="color: #000000;">) progresstrackcolor{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_progresstrackcolor;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) setprogressvalue: (cgfloat) progressvalue{_progressvalue</span></span>=<span style="color: #000000;"><span style="color: #000000;">progressvalue; Frontfillbezierpath</span></span>= [uibezierpath BezierPathWithArcCenter:self.center radius: (cgrectgetwidth (self.bounds)-self.progressstrokewidth) /<span style="color: #800080;"><span style="color: #800080;">2</span></span>. F Startangle:-m_pi_4 Endangle: (<span style="color: #800080;"><span style="color: #800080;">2</span></span>*m_pi) *progressvalue-<span style="color: #000000;"><span style="color: #000000;">M_pi_4 clockwise:yes]; Frontfilllayer.path</span></span>=<span style="color: #000000;"><span style="color: #000000;">frontfillbezierpath.cgpath;}</span></span>-<span style="color: #000000;"><span style="color: #000000;">(cgfloat) progressvalue{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_progressvalue;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) setprogressstrokewidth: (cgfloat) progressstrokewidth{_progressstrokewidth</span></span>=<span style="color: #000000;"><span style="color: #000000;">progressstrokewidth; Frontfilllayer.linewidth</span></span>=<span style="color: #000000;"><span style="color: #000000;">progressstrokewidth; Backgroundlayer.linewidth</span></span>=<span style="color: #000000;"><span style="color: #000000;">progressstrokewidth;}</span></span>-<span style="color: #000000;"><span style="color: #000000;">(cgfloat) progressstrokewidth{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_progressstrokewidth;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">SNCircleProgressView.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller () {sncircleprogressview</span></span>*<span style="color: #000000;"><span style="color: #000000;">progressview;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; Progressview</span></span>= [[sncircleprogressview Alloc]initwithframe:cgrectmake (<span style="color: #800080;"><span style="color: #800080;"></span> +</span>,<span style="color: #800080;"><span style="color: #800080;"></span> the</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">)]; Progressview.progresscolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor redcolor]; Progressview.progressstrokewidth</span></span>=<span style="color: #800080;"><span style="color: #800080;">5</span></span><span style="color: #000000;"><span style="color: #000000;">. f; Progressview.progresstrackcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor whitecolor]; [self.view addsubview:progressview]; [nstimer Scheduledtimerwithtimeinterval:</span></span><span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">. F target:self selector: @selector (changeprogressvalue) userinfo:nil repeats:yes]; }</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Changeprogressvalue{progressview.progressvalue</span></span>+=<span style="color: #800080;"><span style="color: #800080;">0.1</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(progressview.progressvalue>=<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">. F) {progressview.progressvalue</span></span>=<span style="color: #800080;"><span style="color: #800080;">0.1f</span></span><span style="color: #000000;"><span style="color: #000000;">; }}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) didreceivememorywarning {[super didreceivememorywarning]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dispose of any resources the can be Recreated.</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p><p>iOS Draw Ring progress bar</p></p></span>

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.