@@ -18,37 +18,66 @@ import UIKit
18
18
19
19
class CYArcProgressBar : CYBaseProgressBar {
20
20
21
+ // 整个bar的其实角度
21
22
private( set) var startAngle : Double
23
+ // 计算进度的起始角度,在calculateStartAngle和startAngle之间的部分都是已完成状态
24
+ private( set) var calculateStartAngle : Double
22
25
private( set) var endAngle : Double
23
26
private( set) var arcRadius : Double
24
27
private( set) var arcCenter : CGPoint
25
28
26
29
private var totalAngle : Double
27
30
28
31
init ( startAngle: Double ,
32
+ calculateStartAngle: Double ,
29
33
endAngle: Double ,
30
34
arcRadius: Double ,
31
35
arcCenter: CGPoint ,
32
36
barWidth: Double ,
33
37
frame: CGRect ) {
38
+
34
39
self . startAngle = startAngle
40
+ self . calculateStartAngle = calculateStartAngle
35
41
self . endAngle = endAngle
36
42
self . arcRadius = arcRadius
37
43
self . arcCenter = arcCenter
38
44
39
45
// compute total Angle
40
- totalAngle = endAngle - startAngle
46
+ totalAngle = endAngle - calculateStartAngle
41
47
if totalAngle < 0 {
42
48
totalAngle += 2 * M_PI
43
49
}
44
50
super. init ( barWidth: barWidth, frame: frame)
45
51
}
46
52
47
- override convenience init ( barWidth: Double , frame: CGRect ) {
48
- self . init ( startAngle: 0 ,
49
- endAngle: M_PI * 1.5 ,
50
- arcRadius: Double ( min ( frame. width, frame. height) ) / 2 - barWidth / 2 ,
51
- arcCenter: CGPoint ( x: frame. width / 2.0 , y: frame. height / 2.0 ) ,
53
+ convenience init ( startAngle: Double ,
54
+ calculateStartAngle: Double ,
55
+ endAngle: Double ,
56
+ arcRadius: Double ,
57
+ arcCenter: CGPoint ,
58
+ frame: CGRect ) {
59
+
60
+ self . init ( startAngle: startAngle,
61
+ calculateStartAngle: calculateStartAngle,
62
+ endAngle: endAngle,
63
+ arcRadius: arcRadius,
64
+ arcCenter: arcCenter,
65
+ barWidth: 4 ,
66
+ frame: frame)
67
+ }
68
+
69
+ convenience init ( startAngle: Double ,
70
+ endAngle: Double ,
71
+ arcRadius: Double ,
72
+ arcCenter: CGPoint ,
73
+ barWidth: Double ,
74
+ frame: CGRect ) {
75
+
76
+ self . init ( startAngle: startAngle,
77
+ calculateStartAngle: startAngle,
78
+ endAngle: endAngle,
79
+ arcRadius: arcRadius,
80
+ arcCenter: arcCenter,
52
81
barWidth: barWidth,
53
82
frame: frame)
54
83
}
@@ -65,6 +94,15 @@ class CYArcProgressBar: CYBaseProgressBar {
65
94
barWidth: 4.0 ,
66
95
frame: frame)
67
96
}
97
+
98
+ override convenience init ( barWidth: Double , frame: CGRect ) {
99
+ self . init ( startAngle: 0 ,
100
+ endAngle: M_PI * 1.5 ,
101
+ arcRadius: Double ( min ( frame. width, frame. height) ) / 2 - barWidth / 2 ,
102
+ arcCenter: CGPoint ( x: frame. width / 2.0 , y: frame. height / 2.0 ) ,
103
+ barWidth: barWidth,
104
+ frame: frame)
105
+ }
68
106
69
107
required init ? ( coder aDecoder: NSCoder ) {
70
108
@@ -86,8 +124,8 @@ class CYArcProgressBar: CYBaseProgressBar {
86
124
87
125
private func refreshHeaderPosition( ) {
88
126
89
- let endFactorW = cos ( progress * totalAngle + startAngle )
90
- let endFactorH = sin ( progress * totalAngle + startAngle )
127
+ let endFactorW = cos ( progress * totalAngle + calculateStartAngle )
128
+ let endFactorH = sin ( progress * totalAngle + calculateStartAngle )
91
129
progressHeaderView? . center = CGPoint ( x: endFactorW * arcRadius + Double( arcCenter. x) ,
92
130
y: endFactorH * arcRadius + Double( arcCenter. y) )
93
131
}
@@ -107,20 +145,20 @@ class CYArcProgressBar: CYBaseProgressBar {
107
145
let path = UIBezierPath ( arcCenter: arcCenter,
108
146
radius: CGFloat ( arcRadius) ,
109
147
startAngle: CGFloat ( startAngle) ,
110
- endAngle: CGFloat ( progress * totalAngle + startAngle ) ,
148
+ endAngle: CGFloat ( progress * totalAngle + calculateStartAngle ) ,
111
149
clockwise: true )
112
150
completionLayer? . path = path. cgPath
113
- // completedPath = path
151
+ completedPath = path. cgPath
114
152
}
115
153
116
154
override func addAnimation( ) {
117
155
super. addAnimation ( )
118
156
119
- // let animation = CAKeyframeAnimation(keyPath: "position")
120
- // animation.path = completedPath?.cgPath
121
- // animation.calculationMode = kCAAnimationPaced
122
- // animation.duration = 2
123
- // progressHeaderView?.layer.add(animation, forKey: nil)
157
+ let animation = CAKeyframeAnimation ( keyPath: " position " )
158
+ animation. path = completedPath
159
+ animation. calculationMode = kCAAnimationPaced
160
+ animation. duration = 2
161
+ progressHeaderView? . layer. add ( animation, forKey: nil )
124
162
}
125
163
}
126
164
0 commit comments