Swift Circular gradient progress bar support events support Xib and storyboard

Source: Internet
Author: User
Tags linecap

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

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.