swift-圆形进度条的简单封装view类

     class CirCle:UIView{
       //进度条宽度
       let lineWidth: CGFloat = 2
       //进度槽颜色
       let trackColor = UIColor.black
       //进度条颜色
       let progressColoar = UIColor.red
       //进度槽
       let trackLayer = CAShapeLayer()
       //进度条
       let progressLayer = CAShapeLayer()
       //进度条路径(整个圆圈)
       let path = UIBezierPath()
       
       //当前进度
       @IBInspectable var progress: Double = 0 {
           didSet {
               if progress > 100 {
                   progress = 100
               }else if progress < 0 {
                   progress = 0
               }
           }
       }
       
       required init?(coder aDecoder: NSCoder) {
           super.init(coder: aDecoder)
       }
       
       override init(frame: CGRect) {
           super.init(frame: frame)
       }
       
       override func draw(_ rect: CGRect) {
           //获取整个进度条圆圈路径
           path.addArc(withCenter: CGPoint(x: bounds.midX, y: bounds.midY),
                       radius: bounds.size.width/2 - lineWidth,
                       startAngle: angleToRadian(-90), endAngle: angleToRadian(270), clockwise: true)
           
           //绘制进度槽
           trackLayer.frame = bounds
           trackLayer.fillColor = UIColor.clear.cgColor
           trackLayer.strokeColor = trackColor.cgColor
           trackLayer.lineWidth = lineWidth
           trackLayer.path = path.cgPath
           layer.addSublayer(trackLayer)
           
           //绘制进度条
           progressLayer.frame = bounds
           progressLayer.fillColor = UIColor.clear.cgColor
           progressLayer.strokeColor = progressColoar.cgColor
           progressLayer.lineWidth = lineWidth
           progressLayer.path = path.cgPath
           progressLayer.strokeStart = 0
           progressLayer.strokeEnd = CGFloat(progress)/100.0
           layer.addSublayer(progressLayer)
       }
       
       //设置进度(可以设置是否播放动画)
       func setProgress(_ pro: Double,animated anim: Bool) {
           setProgress(pro, animated: anim, withDuration: 0.55)
       }
       
       //设置进度(可以设置是否播放动画,以及动画时间)
       func setProgress(_ pro: Double,animated anim: Bool, withDuration duration: Double) {
           progress = pro
           //进度条动画
           CATransaction.begin()
           CATransaction.setDisableActions(!anim)
           CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name:
               CAMediaTimingFunctionName.easeInEaseOut))
           CATransaction.setAnimationDuration(duration)
           progressLayer.strokeEnd = CGFloat(progress)/100.0
           
           CATransaction.commit()
       }
       
       //将角度转为弧度
       fileprivate func angleToRadian(_ angle: Double)->CGFloat {
           return CGFloat(angle/Double(180.0) * M_PI)
       }
}

你可能感兴趣的:(swift-圆形进度条的简单封装view类)