The default progress bar in iOS is the horizontal progress bar, which often does not meet our needs. But we can customize a similar circular progress hint control, mainly using the drawing mechanism in iOS. Here we are going to implement a continuous increase in the effect of a button click and then a circular progress hint.
(1) Create a new cocoa Touch Class, and note that you want to inherit from UIView. This is the drawing class that draws a circular background and a scalloped progress. 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 as white;} Required init (coder Adecoder:nscoder) {Super.init (Coder:adecoder)} private var _progressvalue:cgfloat = 0//This is the current progress; Func Getprogressval UE ()->cgfloat{return _progressvalue} func setprogressvalue (value:cgfloat) {//Set progress; _PR Ogressvalue = value Setneedsdisplay ()} override func DrawRect (rect:cgrect) {//Draw circular background and sector progress; var context = Uigraphicsgetcurrentcontext () var r = Rect.width/2 Cgcontextaddarc ( Context, R, R, r, 0, 3.1415926 * 2, 0) cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 1) cgcontextfillpat H (context) Cgcontextaddarc (context, R, R, r, 0, 3.1415926 * 2 * _progressvalue, 0) cgcontextaddlinet Opoint (Context, R, R) Cgcontextsetrgbfillcolor (context, 0, 0, 1, 1) cgcontextfillpath (context)}}
(2) Drag a button in the interface to drag the action event. The following are implemented in Viewcontroller:
Import Uikitclass Viewcontroller:uiviewcontroller { var progresscontrol:progresscontrol! Override Func Viewdidload () { super.viewdidload () Progresscontrol = Progresscontrol (Frame:cgrect (x:100, y: (width:100, height:100)) Self.view.addSubview (Progresscontrol) } //Click the button to increase the progress @IBAction func Addprogressvaluepressed (Sender:uibutton) { Progresscontrol.setprogressvalue ( Progresscontrol.getprogressvalue () +0.1) } override Func didreceivememorywarning () { Super.didreceivememorywarning () //Dispose of any resources, can be recreated. }}
(3) The result of the final implementation is as follows:
。
。
For other triggering events, you can also use the custom circular progress control to prompt.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS project development-Custom circular progress tip control