IOS to achieve a simple progress bar function _ios

Source: Internet
Author: User

This article has drawn a cool download progress bar, share for everyone to reference, the specific content as follows

First, the realization of ideas
1. 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 this method
2, if you want to display the download progress, only need to instantiate the object of the subclass (if the control in the storyboard, just modify the control's class property for the custom subclass class name)

3, the effect of the results shown in fact is to draw a circular arc, dynamic change the end of the position, and finally reached a closed circle
4, the middle of the text is a Uilabel control, according to the progress of dynamic changes in the reality of the text
Second, the realization step
1. Customizing a UIView subclass

Provides a member property that receives the download progress value
@property (nonatomic, assign) cgfloat progress;

2. Overriding member properties 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;

3, 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 an arc
  uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center radius:radius-5 Startangle:startangle Endangle:endangle Clockwise:yes];
  Renders a drawn arc onto a layer (that is, displayed)
  [path stroke];
}

The above is the entire content of this article, I hope to help you learn.

Related Article

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.