Swift 简单的环形进度条

在以往的项目中,简单的用到了环形进度条这一功能,现在贴出代码:

import UIKit

struct ProgressProgerty {
    var width : CGFloat?
    var trackColor : UIColor?
    var progressColor : UIColor?
    var progressStart : CGFloat?
    var progressEnd : CGFloat?
    
    init(width:CGFloat, progressEnd:CGFloat, progressColor:UIColor) {
        self.width = width
        self.progressEnd = progressEnd
        self.progressColor = progressColor
        trackColor = UIColor.gray
        progressStart = 0.0
    }
    
    init() {
        width = 5
        trackColor = UIColor.gray
        progressColor = UIColor.red
        progressStart = 0.0
        progressEnd = 1
    }
    
}

class ProgressView: UIView {
    var progressProperty = ProgressProgerty.init()
    private let progressLayer = CAShapeLayer()
    
    init(propressProperty:ProgressProgerty,frame:CGRect) {
        self.progressProperty = propressProperty
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:)has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        let path = UIBezierPath.init(ovalIn: bounds).cgPath
        let tracklayer = CAShapeLayer()
        tracklayer.frame = bounds
        tracklayer.fillColor = UIColor.clear.cgColor
        tracklayer.strokeColor = UIColor.cyan.cgColor
        tracklayer.lineWidth = progressProperty.width!
        tracklayer.path = path
        layer.addSublayer(tracklayer)
        
        progressLayer.frame = bounds
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = progressProperty.progressColor?.cgColor
        progressLayer.lineWidth = progressProperty.width!
        progressLayer.path = path
        progressLayer.strokeStart = progressProperty.progressStart!
        progressLayer.strokeEnd = progressProperty.progressEnd!
        layer.addSublayer(progressLayer)
    }

    func setProgress(progress:CGFloat,time:CFTimeInterval,animate:Bool){
        CATransaction.begin()
        CATransaction.setDisableActions(!animate)
        CATransaction.setAnimationDuration(time)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut))
        progressLayer.strokeEnd = progress
        CATransaction.commit()
    }

}

你可能感兴趣的:(Swift 简单的环形进度条)