Blame view

HDFwear/Basic/View/CircleProgressView.swift 2.59 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  //
  //  CircleProgressView.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/30.
  //
  
  import UIKit
  
  class CircleProgressView: UIView {
  
      
      private lazy var bgLayer: CAShapeLayer = CAShapeLayer()
      // 延迟初始化进度条层采用 stroke 来绘制边框
      private lazy var progressLayer: CAShapeLayer = CAShapeLayer()
      
      var value: Int = 0 {
          didSet {
              progressLayer.strokeEnd = CGFloat(value)/100
          }
      }
      
      @IBOutlet weak var label: UILabel!
      
      
      override init(frame: CGRect) {
          super.init(frame: frame)
          initFromNib()
          setupUI()
      }
      
      
      required init?(coder: NSCoder) {
          super.init(coder: coder)
          initFromNib()
          setupUI()
      }
      override class func awakeFromNib() {
          super.awakeFromNib()
         
      }
      
      private func setupUI() {
          // 0.25 透明度的白色背景
          bgLayer.fillColor = LineColor.cgColor
          layer.addSublayer(bgLayer)
          
          // 边框全白边框宽度为 4
          progressLayer = CAShapeLayer()
          progressLayer.fillColor = nil
          progressLayer.strokeColor = UIColor.rgbColorFromHex(0x1DDCC5).cgColor
          progressLayer.lineWidth = self.frame.width/2
          layer.addSublayer(progressLayer)
          
          
          let radius = bounds.width / 2
          bgLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: bounds.width, height: bounds.width), cornerRadius: radius).cgPath
  
          // 设置 start  12点钟方向开始默认是3点钟方向
          // end = 360 * progress - start
          // 设置为 顺时针 方向
          let end = CGFloat(Double.pi * 2) - CGFloat(Double.pi / 2)
          progressLayer.path = UIBezierPath(arcCenter: CGPoint(x: radius, y: radius), radius: radius/2,
                                            startAngle: -CGFloat(Double.pi / 2), endAngle: end,
                                            clockwise: true).cgPath
          progressLayer.strokeEnd = 0
      }
      
      private func initFromNib() {
          if let view = UINib(nibName: "CircleProgressView", bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil).first as? UIView {
              view.frame = bounds
              self.addSubview(view)
          }
      }
      
      override func layoutSubviews() {
          refreshUI()
      }
      
      private func refreshUI() {
  
          
      }
      
      
      /*
      // Only override draw() if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      override func draw(_ rect: CGRect) {
          // Drawing code
      }
      */
  
  }