IOS project development practice-custom circular progress prompt control

Source: Internet
Author: User

IOS project development practice-custom circular progress prompt control

The default progress bar in iOS is a horizontal progress bar, which often cannot meet our needs. However, we can customize a progress prompt control similar to the circular one, which is mainly implemented using the Drawing Mechanism in iOS. Here, we need to implement the effect of clicking through the button and then the circular progress prompt continuously increasing.

(1) create a Cocoa Touch Class and inherit from UIView. This is the class for drawing graphics, drawing a circular background and the progress of the slice. The specific implementation is as follows:

 

Import UIKitclass ProgressControl: UIView {override init (frame: CGRect) {super. init (frame: frame) self. backgroundColor = UIColor (white: 1, alpha: 0) // initialize the drawing background to white;} required init (coder aDecoder: NSCoder) {super. init (coder: aDecoder)} private var _ progressValue: CGFloat = 0 // This is the current progress; func getProgressValue ()-> CGFloat {return _ progressValue} func setProgressvalue (value: CGFloat) {// set the progress; _ progressValue = value setNeedsDisplay ()} override func drawRect (rect: CGRect) {// draw the circular background and slice progress; var context = UIGraphicsGetCurrentContext () var r = rect. width/2 CGContextAddArc (context, r, 0, 3.1415926*2, 0) CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 1) CGContextFillPath (context) CGContextAddArc (context, r, 0, 3.1415926*2 * _ progressValue, 0) CGContextAddLineToPoint (context, r, r) CGContextSetRGBFillColor (context, 0, 0, 1, 1) CGContextFillPath (context )}}

(2) drag a button to the interface and drag the Action event. Implement the following in ViewController:

 

 

Import UIKitclass ViewController: UIViewController {var progressControl: ProgressControl! Override func viewDidLoad () {super. viewDidLoad () progressControl = ProgressControl (frame: CGRect (x: 100, y: 100, width: 100, height: 100) self. view. addSubview (progressControl)} // click the button to add Progress @ IBAction func addProgressValuePressed (sender: UIButton) {progressControl. setProgressvalue (progressControl. getProgressValue () + 0.1)} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}

(3) The final implementation result is as follows:

 

.

 

.

 

You can also use this custom circular progress control to prompt other trigger events.

 

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.