From b638783e6115442a3c8aecfb33b749232a4c4866 Mon Sep 17 00:00:00 2001 From: Kaan Dedeoglu Date: Wed, 5 Oct 2016 00:43:58 +0300 Subject: [PATCH] Add paddingPercentage property that controls how much padding is added when calculating radii. --- KDCircularProgress/KDCircularProgress.swift | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/KDCircularProgress/KDCircularProgress.swift b/KDCircularProgress/KDCircularProgress.swift index 1aee9ec..f08c824 100755 --- a/KDCircularProgress/KDCircularProgress.swift +++ b/KDCircularProgress/KDCircularProgress.swift @@ -77,6 +77,14 @@ public class KDCircularProgress: UIView, CAAnimationDelegate { private var radius: CGFloat = 0 { didSet { progressLayer.radius = radius + progressLayer.setNeedsDisplay() + } + } + + @IBInspectable public var paddingPercentage: CGFloat = 20 { + didSet { + paddingPercentage = Utility.clamp(value: paddingPercentage, minMax: (0, 100)) + radius = (frame.size.width/2.0) * ((100 - paddingPercentage) / 100) } } @@ -218,11 +226,11 @@ public class KDCircularProgress: UIView, CAAnimationDelegate { public override func layoutSubviews() { super.layoutSubviews() - radius = (frame.size.width/2.0) * 0.8 + radius = (frame.size.width/2.0) * ((100 - paddingPercentage) / 100) } private func setInitialValues() { - radius = (frame.size.width/2.0) * 0.8 //We always apply a 20% padding, stopping glows from being clipped + radius = (frame.size.width/2.0) * ((100 - paddingPercentage) / 100) backgroundColor = .clear set(colors: .white, .cyan) } @@ -424,8 +432,25 @@ public class KDCircularProgress: UIView, CAAnimationDelegate { let trackLineWidth = radius * trackThickness let progressLineWidth = radius * progressThickness + + let trackRadius: CGFloat + let progressRadius: CGFloat + + if trackLineWidth < progressLineWidth { + trackRadius = radius - trackLineWidth/2 - (progressLineWidth - trackLineWidth)/2 + progressRadius = radius - progressLineWidth/2 +// trackLineWidth -= (progressLineWidth - trackLineWidth)/2 + } else if progressLineWidth < trackLineWidth { + progressRadius = radius - progressLineWidth/2 - (trackLineWidth - progressLineWidth)/2 + trackRadius = radius - trackLineWidth/2 +// progressLineWidth -= (trackLineWidth - progressLineWidth)/2 + } else { + progressRadius = radius - progressLineWidth/2 + trackRadius = radius - trackLineWidth/2 + } + let arcRadius = max(radius - trackLineWidth/2, radius - progressLineWidth/2) - ctx.addArc(center: CGPoint(x: width/2.0, y: height/2.0), radius: arcRadius, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: false) + ctx.addArc(center: CGPoint(x: width/2.0, y: height/2.0), radius: trackRadius, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: false) trackColor.set() ctx.setStrokeColor(trackColor.cgColor) ctx.setFillColor(progressInsideFillColor.cgColor) @@ -440,7 +465,7 @@ public class KDCircularProgress: UIView, CAAnimationDelegate { let fromAngle = Conversion.degreesToRadians(value: CGFloat(-startAngle)) let toAngle = Conversion.degreesToRadians(value: CGFloat((clockwise == true ? -reducedAngle : reducedAngle) - startAngle)) - imageCtx?.addArc(center: CGPoint(x: width/2.0, y: height/2.0), radius: arcRadius, startAngle: fromAngle, endAngle: toAngle, clockwise: clockwise) + imageCtx?.addArc(center: CGPoint(x: width/2.0, y: height/2.0), radius: progressRadius, startAngle: fromAngle, endAngle: toAngle, clockwise: clockwise) let glowValue = GlowConstants.glowAmount(forAngle: reducedAngle, glowAmount: glowAmount, glowMode: glowMode, size: width) if glowValue > 0 {