@@ -31,8 +31,8 @@ open class KYCircularProgress: UIView {
31
31
*/
32
32
@IBInspectable open var progress : Double = 0.0 {
33
33
didSet {
34
- let clipProgress = max ( min ( progress, Double ( 1.0 ) ) , Double ( 0.0 ) )
35
- progressView. update ( progress: clipProgress)
34
+ let clipProgress = max ( min ( progress, 1.0 ) , 0.0 )
35
+ progressView. update ( progress: normalize ( progress : clipProgress) )
36
36
37
37
progressChanged ? ( clipProgress, self )
38
38
delegate? . progressChanged ( progress: clipProgress, circularProgress: self )
@@ -105,22 +105,22 @@ open class KYCircularProgress: UIView {
105
105
}
106
106
107
107
/**
108
- Progress start angle.
108
+ Progress start offset. (0.0 - 1.0)
109
109
*/
110
- open var startAngle : Double = 0.0 {
110
+ @ IBInspectable open var strokeStart : Double = 0.0 {
111
111
didSet {
112
- progressView. startAngle = startAngle
113
- guideView. startAngle = startAngle
112
+ progressView. shapeLayer . strokeStart = CGFloat ( max ( min ( strokeStart , 1.0 ) , 0.0 ) )
113
+ guideView. shapeLayer . strokeStart = CGFloat ( max ( min ( strokeStart , 1.0 ) , 0.0 ) )
114
114
}
115
115
}
116
116
117
117
/**
118
- Progress end angle.
118
+ Progress end offset. (0.0 - 1.0)
119
119
*/
120
- open var endAngle : Double = 0 .0 {
120
+ @ IBInspectable open var strokeEnd : Double = 1 .0 {
121
121
didSet {
122
- progressView. endAngle = endAngle
123
- guideView. endAngle = endAngle
122
+ progressView. shapeLayer . strokeEnd = CGFloat ( max ( min ( strokeEnd , 1.0 ) , 0.0 ) )
123
+ guideView. shapeLayer . strokeEnd = CGFloat ( max ( min ( strokeEnd , 1.0 ) , 0.0 ) )
124
124
}
125
125
}
126
126
@@ -175,7 +175,7 @@ open class KYCircularProgress: UIView {
175
175
self . progressView. radius = self . radius
176
176
guideView. shapeLayer. path = self . progressView. shapeLayer. path
177
177
guideView. shapeLayer. strokeColor = self . tintColor. cgColor
178
- guideView. update ( progress: 1.0 )
178
+ guideView. update ( progress: normalize ( progress : 1.0 ) )
179
179
return guideView
180
180
} ( )
181
181
@@ -234,8 +234,8 @@ open class KYCircularProgress: UIView {
234
234
}
235
235
236
236
public func set( progress: Double , duration: Double ) {
237
- let clipProgress = max ( min ( progress, Double ( 1.0 ) ) , Double ( 0.0 ) )
238
- progressView. update ( progress: clipProgress, duration: duration)
237
+ let clipProgress = max ( min ( progress, 1.0 ) , 0.0 )
238
+ progressView. update ( progress: normalize ( progress : clipProgress) , duration: duration)
239
239
240
240
progressChanged ? ( clipProgress, self )
241
241
delegate? . progressChanged ( progress: clipProgress, circularProgress: self )
@@ -248,6 +248,10 @@ open class KYCircularProgress: UIView {
248
248
}
249
249
}
250
250
251
+ private func normalize( progress: Double ) -> CGFloat {
252
+ return CGFloat ( strokeStart + progress * ( strokeEnd - strokeStart) )
253
+ }
254
+
251
255
override open func layoutSubviews( ) {
252
256
super. layoutSubviews ( )
253
257
@@ -267,8 +271,6 @@ public protocol KYCircularProgressDelegate {
267
271
268
272
// MARK: - KYCircularShapeView
269
273
class KYCircularShapeView : UIView {
270
- var startAngle = 0.0
271
- var endAngle = 0.0
272
274
var radius = 0.0
273
275
var scale : ( x: CGFloat , y: CGFloat ) = ( 1.0 , 1.0 )
274
276
@@ -292,27 +294,24 @@ class KYCircularShapeView: UIView {
292
294
override func layoutSubviews( ) {
293
295
super. layoutSubviews ( )
294
296
295
- if startAngle == endAngle {
296
- endAngle = startAngle + ( Double . pi * 2 )
297
- }
298
297
shapeLayer. path = shapeLayer. path ?? layoutPath ( ) . cgPath
299
298
var affineScale = CGAffineTransform ( scaleX: scale. x, y: scale. y)
300
299
shapeLayer. path = shapeLayer. path? . copy ( using: & affineScale)
301
300
}
302
301
303
302
private func layoutPath( ) -> UIBezierPath {
304
303
let halfWidth = CGFloat ( frame. width / 2.0 )
305
- return UIBezierPath ( arcCenter: CGPoint ( x: halfWidth, y: halfWidth) , radius: ( frame. width - CGFloat( radius) ) / 2 , startAngle: CGFloat ( startAngle ) , endAngle: CGFloat ( endAngle ) , clockwise: true )
304
+ return UIBezierPath ( arcCenter: CGPoint ( x: halfWidth, y: halfWidth) , radius: ( frame. width - CGFloat( radius) ) / 2 , startAngle: 0 , endAngle: CGFloat ( Double . pi * 2 ) , clockwise: true )
306
305
}
307
306
308
- fileprivate func update( progress: Double ) {
307
+ fileprivate func update( progress: CGFloat ) {
309
308
CATransaction . begin ( )
310
309
CATransaction . setValue ( kCFBooleanTrue, forKey: kCATransactionDisableActions)
311
- shapeLayer. strokeEnd = CGFloat ( progress)
310
+ shapeLayer. strokeEnd = progress
312
311
CATransaction . commit ( )
313
312
}
314
313
315
- fileprivate func update( progress: Double , duration: Double ) {
314
+ fileprivate func update( progress: CGFloat , duration: Double ) {
316
315
CATransaction . begin ( )
317
316
let animation = CABasicAnimation ( keyPath: " strokeEnd " )
318
317
animation. duration = duration
@@ -321,7 +320,7 @@ class KYCircularShapeView: UIView {
321
320
animation. toValue = progress
322
321
shapeLayer. add ( animation, forKey: " animateStrokeEnd " )
323
322
CATransaction . commit ( )
324
- shapeLayer. strokeEnd = CGFloat ( progress)
323
+ shapeLayer. strokeEnd = progress
325
324
}
326
325
}
327
326
0 commit comments