OC-9. Draw download Progress bar (demo) based on quartz2d _ios

Source: Internet
Author: User

Effect chart

Realize the idea

To implement a drawing, it is often necessary to customize a UIView subclass, overriding the parent-(void) DrawRect: (CGRect) Rect method, which implements drawing operations in the method
To display the download progress, you only need to instantiate a custom subclass object (in the case of a storyboard control, simply modify the control's Class property to a custom subclass name)

The effect of the image shown is actually to draw an arc, the dynamic change in the end of the position, and eventually reached a closed circle

The middle text is a Uilabel control, changing the reality of the text according to the progress dynamically

Implementation steps

Customizing a UIView Subclass

Provides a member property to receive download progress values

@property (nonatomic, assign) cgfloat progress;

overriding a member property progress setter

Each time the value of the member property progress is changed, its setter
-(void) Setprogress: (cgfloat) Progress
{
 _progress = progress is invoked;
 Manually invoke the Redraw method
 [self setneedsdisplay] when the download progress changes;

Rewrite-(void) DrawRect: (cgrect) rect (CORE)

-(void) DrawRect: (cgrect) Rect {//Set radius of arc cgfloat radius = rect.size.width * 0.5;
 Set the center of the arc cgpoint Center = cgpointmake (radius, radius);
 Set the starting angle of the arc (radians) cgfloat startangle =-m_pi_2;
 Set the end angle of the arc cgfloat endangle =-m_pi_2 + 2 * m_pi * self.progress; Use the Uibezierpath class to draw arcs uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center radius:radius-5:
 StartAngle Endangle:endangle Clockwise:yes];
Renders the drawn arc to a layer (that is, displayed) [path stroke]; }

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.