Use quarz2d to draw a progress bar

Source: Internet
Author: User

I have always thought that the progress bar of the iPhone system is a little simple. Due to the actual needs of the project, I drew a progress bar by referring to some materials, mainly using quarz2d. Draw a rectangle, fill in the internal color, stroke, and gradually change the fill color from the left to the right. The final effect is a dynamic progress bar.

First, the header file: customprogressview. h

 

 

 
// Created by suruqiang on 8/4/10. // copy 2010 _ mycompanyname __. all rights reserved. // # import <uikit/uikit. h> @ interface customprogressview: uiview {float progress;} @ property (assign, nonatomic) float progress;-(ID) Init; //-(void) drawrect :( cgrect) _ rect borderradius :( cgfloat) _ rad borderwidth :( cgfloat) _ thickness barradius :( cgfloat) _ barradius barinsert :( cgfloat) _ barinsert;-(void) drawrect :( cgrect) rect borderradius :( cgfloat) rad borderwidth :( cgfloat) thickness barradius :( cgfloat) barradius barinset :( cgfloat) barinset; @ end

 

 

 

Customprogressview. M file implementationCode:

// Created by suruiqiang on 8/4/10. // copy 2010 _ mycompanyname __. all rights reserved. // # import "customprogressview. H "@ implementation customprogressview @ synthesize progress;-(ID) Init {cgrect rect = cgrectmake (0, 0,210, 20); // If (! [Super initwithframe: rect]) // return nil; [Super initwithframe: rect]; Progress = 0; self. backgroundcolor = [uicolor clearcolor]; return self;}-(void) setprogress :( float) _ progress {_ progress = min (max (0, _ progress), 1 ); if (_ progress = progress) return; Progress = _ progress; [self setneedsdisplay];}-(void) drawrect :( cgrect) rect {[self drawrect: rect borderradius: 8. borderwidth: 2. barradius: 5. barinset: 3.];} -(void) drawrect :( cgrect) rect borderradius :( cgfloat) rad borderwidth :( cgfloat) thickness barradius :( cgfloat) barradius barinset :( cgfloat) barinset {// draw the outside rectangle of custom progressviewcgcontextref context = histogram (); cgrect rrect = cgrectinset (rect, thickness, thickness); cgfloat radius = rad; cgfloat Minx = round (rrect), midx = cgrectgetmidx (rrect), Maxx = cgrectgetmaxx (rrect); cgfloat miny = round (rrect), midy = round (rrect ), maxy = cgrectgetmaxy (rrect); cgcontextmovetopoint (context, Minx, midy); Round (context, Minx, miny, midx, miny, radius); cgcontextaddarctopoint (context, Maxx, miny, maxx, midy, radius); cgcontextaddarctopoint (context, Maxx, Maxy, midx, Maxy, radius); cgcontextaddarctopoint (context, Minx, Maxy, Minx, midy, radius ); cgcontextclosepath (context); cgcontextsetrgbstrokecolor (context, 0, 1, 1); // cgcontextsetrgbfillcolor (context, 1, 1, 1); cgcontextsetlinewidth (context, thickness ); cgcontextdrawpath (context, kcgpathstroke); radius = barradius; rrect = cgrectinset (rrect, barinset, barinset); rrect. size. width = rrect. size. width * self. progress; Minx = forward (rrect), midx = cgrectgetmidx (rrect), Maxx = cgrectgetmaxx (rrect); miny = forward (rrect), midy = cgrectgetmidy (rrect ), maxy = cgrectgetmaxy (rrect); cgcontextmovetopoint (context, Minx, midy); Round (context, Minx, miny, midx, miny, radius); cgcontextaddarctopoint (context, Maxx, miny, maxx, midy, radius); cgcontextaddarctopoint (context, Maxx, Maxy, midx, Maxy, radius); cgcontextaddarctopoint (context, Minx, Maxy, Minx, midy, radius ); cgcontextclosepath (context); cgcontextsetrgbfillcolor (context, 1, 0, 1, 1); cgcontextdrawpath (context, kcgpathfill);}-(void) viewdidunload {// release any retained subviews of the main view. // e.g. self. myoutlet = nil;}-(void) dealloc {[Super dealloc];} @ end

 

 

 

The called code is as follows:

 

 
-(Void) viewdidload {[Super viewdidload]; // [self. view addsubview: [self progressbar]; progressbar = [[customprogressview alloc] init]; progressbar. center = cgpointmake (160.0f, 160.0f); [progressbar setprogress: 0.01]; [self. view addsubview: progressbar]; time = 0; timer = [[nst1_timerwithtimeinterval: 0.02 target: Self selector: @ selector (timer) userinfo: Nil repeats: Yes] retain]; [[nsunloop currentrunloop] addtimer: timer formode: nsdefaultrunloopmode];}-(void) timer {self. progressbar. progress = Time/100.0; time ++; If (Time> 130) time =-30; return ;}
 
Because I have just drawn a lot of code and don't have any artist elements, I need to set and optimize the colors in my project.
 

 

 

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.