A recent project has used an experience bar that requires a color ramp in the middle of the user's avatar to accept the event and then write a control of the following
xprogressview.swift//xprogressview////Created by EDUO_XIAOP on 15/4/11.//Copyright (c) 2015 Eduo. All rights Reserved.//import foundationimport UIKIT@OBJC protocol xprogressviewdelegate:nsobjectprotocol {Optional FU NC progresstapped (progressview:xprogressview!)} @IBDesignable class Xprogressview:uiview {struct Constant {static let Linewidth:cgfloat = Ten stat IC Let Trackcolor = Uicolor (red:245/255.0, green:245/255.0, blue:245/255.0, alpha:1) static let EndColor = Uico Lor.redcolor (). Colorwithalphacomponent (0.8) static Let StartColor = Uicolor.greencolor (). colorwithalphacomponent (0. 2)} Let Gradientlayer = Cagradientlayer () Let Tracklayer = Cashapelayer () Let Progresslayer = Cashapelaye R () Let Path = Uibezierpath () @IBInspectable var progress:int = 0 @IBInspectable var image:uiimage? Weak var delegate:xprogressviewdelegate? Required Init (coder Adecoder:nscoder) {super.inIt (Coder:adecoder)} override init (Frame:cgrect) {super.init (Frame:frame)} override func D Rawrect (rect:cgrect) {path.movetopoint (Cgpointmake (Cgrectgetmidx (bounds) + constant.linewidth/2.0, Constant.lineW Idth) Path.addarcwithcenter (Cgpointmake (Cgrectgetmidx (bounds), cgrectgetmidy (bounds)), radius:bounds.size.width/ 2-constant.linewidth, Startangle:angletoradian ( -90), Endangle:angletoradian (+), clockwise:true) Trac Klayer.frame = Bounds Tracklayer.fillcolor = Uicolor.clearcolor (). Cgcolor Tracklayer.strokecolor = Constant.trackColor.CGColor Tracklayer.linewidth = constant.linewidth Tracklayer.linecap = kcalinecapround Tracklayer.path = path. Cgpath Layer.addsublayer (tracklayer) progresslayer.frame = bounds Progresslayer.fillcolor = U Icolor.clearcolor (). Cgcolor Progresslayer.strokecolor = Uicolor.orangecolor (). Cgcolor progresslayer.linewidth = COnstant.linewidth Progresslayer.linecap = kcalinecapsquare Progresslayer.path = path. Cgpath Progresslayer.strokestart = 0 Progresslayer.strokeend = cgfloat (progress)/100.0 Gradie Ntlayer.frame = Bounds Gradientlayer.colors = [Constant.startColor.CGColor, Constant.endColor.CGColor] Gradi Entlayer.mask = Progresslayer Layer.addsublayer (gradientlayer) path.addclip () image?. Drawinrect (Bounds)} func setprogress (pro:int,animated anim:bool) {setprogress (pro, Animated:anim, WI thduration:0.55)} func setprogress (pro:int,animated anim:bool, withduration duration:double) {progr ESS = Pro Catransaction.begin () catransaction.setdisableactions (!anim) Catransaction.setanimationtimi Ngfunction (Camediatimingfunction (name:kcamediatimingfunctioneaseineaseout)) Catransaction.setanimationduration ( Duration) Progresslayer.strokeend = CGFloat (progress)/100.0 Catransaction.commit ()} private func Angletoradian (angle:double)->cgfloat {return C Gfloat (angle/double (180.0) * m_pi)} override func Touchesbegan (Touches:nsset, withevent event:uievent) { If delegate! = Nil && delegate!. Respondstoselector ("progresstapped:") {delegate?. progresstapped! (self)} }}
Swift Circular gradient progress bar support events support Xib and storyboard