-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularProgressView
More file actions
131 lines (107 loc) · 3.71 KB
/
CircularProgressView
File metadata and controls
131 lines (107 loc) · 3.71 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Foundation
import UIKit
class CircularProgressView: UIView {
fileprivate var progressLayer = CAShapeLayer()
fileprivate var trackLayer = CAShapeLayer()
fileprivate var didConfigureLabel = false
fileprivate var rounded: Bool
fileprivate var filled: Bool
fileprivate let lineWidth: CGFloat?
var timeToFill = 3.43
var progressColor = UIColor.white {
didSet{
progressLayer.strokeColor = progressColor.cgColor
}
}
var trackColor = UIColor.white {
didSet{
trackLayer.strokeColor = trackColor.cgColor
}
}
var progress: Float {
didSet{
var pathMoved = progress - oldValue
if pathMoved < 0{
pathMoved = 0 - pathMoved
}
setProgress(duration: timeToFill * Double(pathMoved), to: progress)
}
}
fileprivate func createProgressView(){
self.backgroundColor = .clear
self.layer.cornerRadius = frame.size.width / 2
let circularPath = UIBezierPath(arcCenter: center, radius: frame.width / 2, startAngle: CGFloat(-0.5 * .pi), endAngle: CGFloat(1.5 * .pi), clockwise: true)
trackLayer.fillColor = UIColor.blue.cgColor
trackLayer.path = circularPath.cgPath
trackLayer.fillColor = .none
trackLayer.strokeColor = trackColor.cgColor
if filled {
trackLayer.lineCap = .butt
trackLayer.lineWidth = frame.width
} else {
trackLayer.lineWidth = lineWidth!
}
trackLayer.strokeEnd = 1
layer.addSublayer(trackLayer)
progressLayer.path = circularPath.cgPath
progressLayer.fillColor = .none
progressLayer.strokeColor = progressColor.cgColor
if filled {
progressLayer.lineCap = .butt
progressLayer.lineWidth = frame.width
} else {
progressLayer.lineWidth = lineWidth!
}
progressLayer.strokeEnd = 0
if rounded{
progressLayer.lineCap = .round
}
layer.addSublayer(progressLayer)
}
func trackColorToProgressColor() -> Void{
trackColor = progressColor
trackColor = UIColor(red: progressColor.cgColor.components![0], green: progressColor.cgColor.components![1], blue: progressColor.cgColor.components![2], alpha: 0.2)
}
func setProgress(duration: TimeInterval = 3, to newProgress: Float) -> Void{
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = progressLayer.strokeEnd
animation.toValue = newProgress
progressLayer.strokeEnd = CGFloat(newProgress)
progressLayer.add(animation, forKey: "animationProgress")
}
override init(frame: CGRect){
progress = 0
rounded = true
filled = false
lineWidth = 4
super.init(frame: frame)
filled = false
createProgressView()
}
required init?(coder: NSCoder) {
progress = 0
rounded = true
filled = false
lineWidth = 15
super.init(coder: coder)
createProgressView()
}
init(frame: CGRect, lineWidth: CGFloat?, rounded: Bool) {
progress = 0
if lineWidth == nil{
self.filled = true
self.rounded = false
} else {
if rounded{
self.rounded = true
} else {
self.rounded = false
}
self.filled = false
}
self.lineWidth = lineWidth
super.init(frame: frame)
createProgressView()
}
}