@@ -13,7 +13,7 @@ public enum KDCircularProgressGlowMode {
13
13
}
14
14
15
15
@IBDesignable
16
- public class KDCircularProgress : UIView {
16
+ public class KDCircularProgress : UIView , CAAnimationDelegate {
17
17
18
18
private struct Conversion {
19
19
static func degreesToRadians ( value: CGFloat ) -> CGFloat {
@@ -291,7 +291,7 @@ public class KDCircularProgress: UIView {
291
291
}
292
292
293
293
public func pauseAnimation( ) {
294
- guard let presentationLayer = progressLayer. presentationLayer ( ) as? KDCircularProgressViewLayer else { return }
294
+ guard let presentationLayer = progressLayer. presentationLayer ( ) else { return }
295
295
let currentValue = presentationLayer. angle
296
296
progressLayer. removeAllAnimations ( )
297
297
animationCompletionBlock = nil
@@ -308,10 +308,10 @@ public class KDCircularProgress: UIView {
308
308
return progressLayer. animationForKey ( " angle " ) != nil
309
309
}
310
310
311
- override public func animationDidStop( anim: CAAnimation , finished flag: Bool ) {
311
+ public func animationDidStop( anim: CAAnimation , finished flag: Bool ) {
312
312
if let completionBlock = animationCompletionBlock {
313
313
if flag {
314
- animationCompletionBlock = nil
314
+ animationCompletionBlock = nil
315
315
}
316
316
317
317
completionBlock ( flag)
@@ -439,28 +439,34 @@ public class KDCircularProgress: UIView {
439
439
CGContextDrawPath ( ctx, . FillStroke)
440
440
441
441
UIGraphicsBeginImageContextWithOptions ( size, false , 0.0 )
442
-
443
- let imageCtx = UIGraphicsGetCurrentContext ( )
444
442
let reducedAngle = Utility . mod ( angle, range: 360 , minMax: ( 0 , 360 ) )
445
- let fromAngle = Conversion . degreesToRadians ( CGFloat ( - startAngle) )
446
- let toAngle = Conversion . degreesToRadians ( CGFloat ( ( clockwise == true ? - reducedAngle : reducedAngle) - startAngle) )
447
-
448
- CGContextAddArc ( imageCtx, width/ 2.0 , height/ 2.0 , arcRadius, fromAngle, toAngle, clockwise == true ? 1 : 0 )
449
443
450
- let glowValue = GlowConstants . glowAmountForAngle ( reducedAngle, glowAmount: glowAmount, glowMode: glowMode, size: width)
451
- if glowValue > 0 {
452
- CGContextSetShadowWithColor ( imageCtx, CGSizeZero, glowValue, UIColor . blackColor ( ) . CGColor)
444
+ if let imageCtx = UIGraphicsGetCurrentContext ( ) {
445
+ let fromAngle = Conversion . degreesToRadians ( CGFloat ( - startAngle) )
446
+ let toAngle = Conversion . degreesToRadians ( CGFloat ( ( clockwise == true ? - reducedAngle : reducedAngle) - startAngle) )
447
+
448
+ CGContextAddArc ( imageCtx, width/ 2.0 , height/ 2.0 , arcRadius, fromAngle, toAngle, clockwise == true ? 1 : 0 )
449
+
450
+ let glowValue = GlowConstants . glowAmountForAngle ( reducedAngle, glowAmount: glowAmount, glowMode: glowMode, size: width)
451
+ if glowValue > 0 {
452
+ CGContextSetShadowWithColor ( imageCtx, CGSizeZero, glowValue, UIColor . blackColor ( ) . CGColor)
453
+ }
454
+ CGContextSetLineCap ( imageCtx, roundedCorners == true ? . Round : . Butt)
455
+ CGContextSetLineWidth ( imageCtx, progressLineWidth)
456
+ CGContextDrawPath ( imageCtx, . Stroke)
457
+
458
+ guard let currentGraphicsContext = UIGraphicsGetCurrentContext ( ) else {
459
+ return
460
+ }
461
+
462
+ if let drawMask: CGImageRef = CGBitmapContextCreateImage ( currentGraphicsContext) {
463
+ UIGraphicsEndImageContext ( )
464
+ CGContextSaveGState ( ctx)
465
+ CGContextClipToMask ( ctx, bounds, drawMask)
466
+ } else {
467
+ UIGraphicsEndImageContext ( )
468
+ }
453
469
}
454
- CGContextSetLineCap ( imageCtx, roundedCorners == true ? . Round : . Butt)
455
- CGContextSetLineWidth ( imageCtx, progressLineWidth)
456
- CGContextDrawPath ( imageCtx, . Stroke)
457
-
458
- let drawMask : CGImageRef = CGBitmapContextCreateImage ( UIGraphicsGetCurrentContext ( ) ) !
459
- UIGraphicsEndImageContext ( )
460
-
461
- CGContextSaveGState ( ctx)
462
- CGContextClipToMask ( ctx, bounds, drawMask)
463
-
464
470
//Gradient - Fill
465
471
if !lerpColorMode && colorsArray. count > 1 {
466
472
let rgbColorsArray : [ UIColor ] = colorsArray. map { color in // Make sure every color in colors array is in RGB color space
0 commit comments