Swift 圓形漸層進度條 支援事件 支援XIB和StoryBoard

來源:互聯網
上載者:User

標籤:

最近項目用到了 一個經驗條  要求色彩坡形  中間是帳戶圖片  還要接受事件  然後自己寫了個下面這個控制項

////  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 func progressTapped(progressView: XProgressView!)}@IBDesignable class XProgressView: UIView {        struct Constant {        static let lineWidth: CGFloat = 10        static let trackColor = UIColor(red: 245/255.0, green: 245/255.0, blue: 245/255.0, alpha: 1)        static let endColor = UIColor.redColor().colorWithAlphaComponent(0.8)        static let startColor = UIColor.greenColor().colorWithAlphaComponent(0.2)    }        let gradientLayer = CAGradientLayer()    let trackLayer = CAShapeLayer()    let progressLayer = CAShapeLayer()    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 drawRect(rect: CGRect) {        path.moveToPoint(CGPointMake(CGRectGetMidX(bounds) + Constant.lineWidth/2.0, Constant.lineWidth))        path.addArcWithCenter(CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)), radius: bounds.size.width/2 - Constant.lineWidth, startAngle: angleToRadian(-90), endAngle: angleToRadian(270), clockwise: true)                trackLayer.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 = UIColor.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                gradientLayer.frame = bounds        gradientLayer.colors = [Constant.startColor.CGColor, Constant.endColor.CGColor]        gradientLayer.mask = progressLayer        layer.addSublayer(gradientLayer)                path.addClip()        image?.drawInRect(bounds)    }        func setProgress(pro: Int,animated anim: Bool) {        setProgress(pro, animated: anim, withDuration: 0.55)    }        func setProgress(pro: Int,animated anim: Bool, withDuration duration: Double) {        progress = pro        CATransaction.begin()        CATransaction.setDisableActions(!anim)        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))        CATransaction.setAnimationDuration(duration)        progressLayer.strokeEnd = CGFloat(progress)/100.0        CATransaction.commit()    }        private func angleToRadian(angle: Double)->CGFloat {        return CGFloat(angle/Double(180.0) * M_PI)    }        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {        if delegate != nil && delegate!.respondsToSelector("progressTapped:") {            delegate?.progressTapped!(self)        }    }}


Swift 圓形漸層進度條 支援事件 支援XIB和StoryBoard

相關文章

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.