|
|
|
@@ -15,18 +15,18 @@ public enum KDCircularProgressGlowMode {
|
|
|
|
|
@IBDesignable
|
|
|
|
|
public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
private struct ConversionFunctions {
|
|
|
|
|
static func DegreesToRadians (value:CGFloat) -> CGFloat {
|
|
|
|
|
private struct Conversion {
|
|
|
|
|
static func degreesToRadians (value:CGFloat) -> CGFloat {
|
|
|
|
|
return value * CGFloat(M_PI) / 180.0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func RadiansToDegrees (value:CGFloat) -> CGFloat {
|
|
|
|
|
static func radiansToDegrees (value:CGFloat) -> CGFloat {
|
|
|
|
|
return value * 180.0 / CGFloat(M_PI)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct UtilityFunctions {
|
|
|
|
|
static func Clamp<T: Comparable>(value: T, minMax: (T, T)) -> T {
|
|
|
|
|
private struct Utility {
|
|
|
|
|
static func clamp<T: Comparable>(value: T, minMax: (T, T)) -> T {
|
|
|
|
|
let (min, max) = minMax
|
|
|
|
|
if value < min {
|
|
|
|
|
return min
|
|
|
|
@@ -37,16 +37,16 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func InverseLerp(value: CGFloat, minMax: (CGFloat, CGFloat)) -> CGFloat {
|
|
|
|
|
static func inverseLerp(value: CGFloat, minMax: (CGFloat, CGFloat)) -> CGFloat {
|
|
|
|
|
return (value - minMax.0) / (minMax.1 - minMax.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func Lerp(value: CGFloat, minMax: (CGFloat, CGFloat)) -> CGFloat {
|
|
|
|
|
static func lerp(value: CGFloat, minMax: (CGFloat, CGFloat)) -> CGFloat {
|
|
|
|
|
return (minMax.1 - minMax.0) * value + minMax.0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func ColorLerp(value: CGFloat, minMax: (UIColor, UIColor)) -> UIColor {
|
|
|
|
|
let clampedValue = Clamp(value, minMax: (0, 1))
|
|
|
|
|
static func colorLerp(value: CGFloat, minMax: (UIColor, UIColor)) -> UIColor {
|
|
|
|
|
let clampedValue = clamp(value, minMax: (0, 1))
|
|
|
|
|
|
|
|
|
|
let zero: CGFloat = 0
|
|
|
|
|
|
|
|
|
@@ -56,23 +56,23 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
var (r1, g1, b1, a1) = (zero, zero, zero, zero)
|
|
|
|
|
minMax.1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
|
|
|
|
|
|
|
|
|
|
return UIColor(red: Lerp(clampedValue, minMax: (r0, r1)), green: Lerp(clampedValue, minMax: (g0, g1)), blue: Lerp(clampedValue, minMax: (b0, b1)), alpha: Lerp(clampedValue, minMax: (a0, a1)))
|
|
|
|
|
return UIColor(red: lerp(clampedValue, minMax: (r0, r1)), green: lerp(clampedValue, minMax: (g0, g1)), blue: lerp(clampedValue, minMax: (b0, b1)), alpha: lerp(clampedValue, minMax: (a0, a1)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func Mod(value: Int, range: Int, minMax: (Int, Int)) -> Int {
|
|
|
|
|
static func mod(value: Double, range: Double, minMax: (Double, Double)) -> Double {
|
|
|
|
|
let (min, max) = minMax
|
|
|
|
|
assert(abs(range) <= abs(max - min), "range should be <= than the interval")
|
|
|
|
|
if value >= min && value <= max {
|
|
|
|
|
return value
|
|
|
|
|
} else if value < min {
|
|
|
|
|
return Mod(value + range, range: range, minMax: minMax)
|
|
|
|
|
return mod(value + range, range: range, minMax: minMax)
|
|
|
|
|
} else {
|
|
|
|
|
return Mod(value - range, range: range, minMax: minMax)
|
|
|
|
|
return mod(value - range, range: range, minMax: minMax)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var progressLayer: KDCircularProgressViewLayer! {
|
|
|
|
|
private var progressLayer: KDCircularProgressViewLayer {
|
|
|
|
|
get {
|
|
|
|
|
return layer as! KDCircularProgressViewLayer
|
|
|
|
|
}
|
|
|
|
@@ -84,7 +84,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var angle: Int = 0 {
|
|
|
|
|
@IBInspectable public var angle: Double = 0 {
|
|
|
|
|
didSet {
|
|
|
|
|
if self.isAnimating() {
|
|
|
|
|
self.pauseAnimation()
|
|
|
|
@@ -93,9 +93,10 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var startAngle: Int = 0 {
|
|
|
|
|
@IBInspectable public var startAngle: Double = 0 {
|
|
|
|
|
didSet {
|
|
|
|
|
progressLayer.startAngle = UtilityFunctions.Mod(startAngle, range: 360, minMax: (0,360))
|
|
|
|
|
startAngle = Utility.mod(startAngle, range: 360, minMax: (0, 360))
|
|
|
|
|
progressLayer.startAngle = startAngle
|
|
|
|
|
progressLayer.setNeedsDisplay()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -127,7 +128,8 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var glowAmount: CGFloat = 1.0 {//Between 0 and 1
|
|
|
|
|
didSet {
|
|
|
|
|
progressLayer.glowAmount = UtilityFunctions.Clamp(glowAmount, minMax: (0, 1))
|
|
|
|
|
glowAmount = Utility.clamp(glowAmount, minMax: (0, 1))
|
|
|
|
|
progressLayer.glowAmount = glowAmount
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -139,19 +141,19 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var progressThickness: CGFloat = 0.4 {//Between 0 and 1
|
|
|
|
|
didSet {
|
|
|
|
|
progressThickness = UtilityFunctions.Clamp(progressThickness, minMax: (0, 1))
|
|
|
|
|
progressThickness = Utility.clamp(progressThickness, minMax: (0, 1))
|
|
|
|
|
progressLayer.progressThickness = progressThickness/2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var trackThickness: CGFloat = 0.5 {//Between 0 and 1
|
|
|
|
|
didSet {
|
|
|
|
|
trackThickness = UtilityFunctions.Clamp(trackThickness, minMax: (0, 1))
|
|
|
|
|
trackThickness = Utility.clamp(trackThickness, minMax: (0, 1))
|
|
|
|
|
progressLayer.trackThickness = trackThickness/2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBInspectable public var trackColor: UIColor = UIColor.blackColor() {
|
|
|
|
|
@IBInspectable public var trackColor: UIColor = .blackColor() {
|
|
|
|
|
didSet {
|
|
|
|
|
progressLayer.trackColor = trackColor
|
|
|
|
|
progressLayer.setNeedsDisplay()
|
|
|
|
@@ -163,7 +165,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
if let color = progressInsideFillColor {
|
|
|
|
|
progressLayer.progressInsideFillColor = color
|
|
|
|
|
} else {
|
|
|
|
|
progressLayer.progressInsideFillColor = UIColor.clearColor()
|
|
|
|
|
progressLayer.progressInsideFillColor = .clearColor()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -184,7 +186,6 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
@objc @IBInspectable private var IBColor2: UIColor?
|
|
|
|
|
@objc @IBInspectable private var IBColor3: UIColor?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var animationCompletionBlock: ((Bool) -> Void)?
|
|
|
|
|
|
|
|
|
|
override public init(frame: CGRect) {
|
|
|
|
@@ -224,17 +225,17 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
private func setInitialValues() {
|
|
|
|
|
radius = (frame.size.width/2.0) * 0.8 //We always apply a 20% padding, stopping glows from being clipped
|
|
|
|
|
backgroundColor = .clearColor()
|
|
|
|
|
setColors(UIColor.whiteColor(), UIColor.redColor())
|
|
|
|
|
setColors(.whiteColor(), .cyanColor())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func refreshValues() {
|
|
|
|
|
progressLayer.angle = angle
|
|
|
|
|
progressLayer.startAngle = UtilityFunctions.Mod(startAngle, range: 360, minMax: (0,360))
|
|
|
|
|
progressLayer.startAngle = startAngle
|
|
|
|
|
progressLayer.clockwise = clockwise
|
|
|
|
|
progressLayer.roundedCorners = roundedCorners
|
|
|
|
|
progressLayer.lerpColorMode = lerpColorMode
|
|
|
|
|
progressLayer.gradientRotateSpeed = gradientRotateSpeed
|
|
|
|
|
progressLayer.glowAmount = UtilityFunctions.Clamp(glowAmount, minMax: (0, 1))
|
|
|
|
|
progressLayer.glowAmount = glowAmount
|
|
|
|
|
progressLayer.glowMode = glowMode
|
|
|
|
|
progressLayer.progressThickness = progressThickness/2
|
|
|
|
|
progressLayer.trackColor = trackColor
|
|
|
|
@@ -242,8 +243,8 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func checkAndSetIBColors() {
|
|
|
|
|
let nonNilColors = [IBColor1, IBColor2, IBColor3].filter { $0 != nil}.map { $0! }
|
|
|
|
|
if nonNilColors.count > 0 {
|
|
|
|
|
let nonNilColors = [IBColor1, IBColor2, IBColor3].flatMap { $0 }
|
|
|
|
|
if !nonNilColors.isEmpty {
|
|
|
|
|
setColors(nonNilColors)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -257,7 +258,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
progressLayer.setNeedsDisplay()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func animateFromAngle(fromAngle: Int, toAngle: Int, duration: NSTimeInterval, relativeDuration: Bool = true, completion: ((Bool) -> Void)?) {
|
|
|
|
|
public func animateFromAngle(fromAngle: Double, toAngle: Double, duration: NSTimeInterval, relativeDuration: Bool = true, completion: ((Bool) -> Void)?) {
|
|
|
|
|
if isAnimating() {
|
|
|
|
|
pauseAnimation()
|
|
|
|
|
}
|
|
|
|
@@ -266,7 +267,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
if relativeDuration {
|
|
|
|
|
animationDuration = duration
|
|
|
|
|
} else {
|
|
|
|
|
let traveledAngle = UtilityFunctions.Mod(toAngle - fromAngle, range: 360, minMax: (0, 360))
|
|
|
|
|
let traveledAngle = Utility.mod(toAngle - fromAngle, range: 360, minMax: (0, 360))
|
|
|
|
|
let scaledDuration = (NSTimeInterval(traveledAngle) * duration) / 360
|
|
|
|
|
animationDuration = scaledDuration
|
|
|
|
|
}
|
|
|
|
@@ -282,7 +283,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
progressLayer.addAnimation(animation, forKey: "angle")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func animateToAngle(toAngle: Int, duration: NSTimeInterval, relativeDuration: Bool = true, completion: ((Bool) -> Void)?) {
|
|
|
|
|
public func animateToAngle(toAngle: Double, duration: NSTimeInterval, relativeDuration: Bool = true, completion: ((Bool) -> Void)?) {
|
|
|
|
|
if isAnimating() {
|
|
|
|
|
pauseAnimation()
|
|
|
|
|
}
|
|
|
|
@@ -298,6 +299,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func stopAnimation() {
|
|
|
|
|
animationCompletionBlock = nil
|
|
|
|
|
progressLayer.removeAllAnimations()
|
|
|
|
|
angle = 0
|
|
|
|
|
}
|
|
|
|
@@ -308,8 +310,11 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
override public func animationDidStop(anim: CAAnimation, finished flag: Bool) {
|
|
|
|
|
if let completionBlock = animationCompletionBlock {
|
|
|
|
|
if flag {
|
|
|
|
|
animationCompletionBlock = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
completionBlock(flag)
|
|
|
|
|
animationCompletionBlock = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -333,13 +338,13 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class KDCircularProgressViewLayer: CALayer {
|
|
|
|
|
@NSManaged var angle: Int
|
|
|
|
|
@NSManaged var angle: Double
|
|
|
|
|
var radius: CGFloat! {
|
|
|
|
|
didSet {
|
|
|
|
|
invalidateGradientCache()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var startAngle: Int!
|
|
|
|
|
var startAngle: Double!
|
|
|
|
|
var clockwise: Bool! {
|
|
|
|
|
didSet {
|
|
|
|
|
if clockwise != oldValue {
|
|
|
|
@@ -370,7 +375,7 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
private struct GlowConstants {
|
|
|
|
|
private static let sizeToGlowRatio: CGFloat = 0.00015
|
|
|
|
|
static func glowAmountForAngle(angle: Int, glowAmount: CGFloat, glowMode: KDCircularProgressGlowMode, size: CGFloat) -> CGFloat {
|
|
|
|
|
static func glowAmountForAngle(angle: Double, glowAmount: CGFloat, glowMode: KDCircularProgressGlowMode, size: CGFloat) -> CGFloat {
|
|
|
|
|
switch glowMode {
|
|
|
|
|
case .Forward:
|
|
|
|
|
return CGFloat(angle) * size * sizeToGlowRatio * glowAmount
|
|
|
|
@@ -417,13 +422,15 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
override func drawInContext(ctx: CGContext) {
|
|
|
|
|
UIGraphicsPushContext(ctx)
|
|
|
|
|
let rect = bounds
|
|
|
|
|
let size = rect.size
|
|
|
|
|
|
|
|
|
|
let trackLineWidth: CGFloat = radius * trackThickness
|
|
|
|
|
let size = bounds.size
|
|
|
|
|
let width = size.width
|
|
|
|
|
let height = size.height
|
|
|
|
|
|
|
|
|
|
let trackLineWidth = radius * trackThickness
|
|
|
|
|
let progressLineWidth = radius * progressThickness
|
|
|
|
|
let arcRadius = max(radius - trackLineWidth/2, radius - progressLineWidth/2)
|
|
|
|
|
CGContextAddArc(ctx, CGFloat(size.width/2.0), CGFloat(size.height/2.0), arcRadius, 0, CGFloat(M_PI * 2), 0)
|
|
|
|
|
CGContextAddArc(ctx, width/2.0, height/2.0, arcRadius, 0, CGFloat(M_PI * 2), 0)
|
|
|
|
|
trackColor.set()
|
|
|
|
|
CGContextSetStrokeColorWithColor(ctx, trackColor.CGColor)
|
|
|
|
|
CGContextSetFillColorWithColor(ctx, progressInsideFillColor.CGColor)
|
|
|
|
@@ -432,12 +439,15 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
CGContextDrawPath(ctx, .FillStroke)
|
|
|
|
|
|
|
|
|
|
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
|
|
|
|
|
|
|
|
|
|
let imageCtx = UIGraphicsGetCurrentContext()
|
|
|
|
|
let reducedAngle = UtilityFunctions.Mod(angle, range: 360, minMax: (0, 360))
|
|
|
|
|
let fromAngle = ConversionFunctions.DegreesToRadians(CGFloat(-startAngle))
|
|
|
|
|
let toAngle = ConversionFunctions.DegreesToRadians(CGFloat((clockwise == true ? -reducedAngle : reducedAngle) - startAngle))
|
|
|
|
|
CGContextAddArc(imageCtx, CGFloat(size.width/2.0),CGFloat(size.height/2.0), arcRadius, fromAngle, toAngle, clockwise == true ? 1 : 0)
|
|
|
|
|
let glowValue = GlowConstants.glowAmountForAngle(reducedAngle, glowAmount: glowAmount, glowMode: glowMode, size: size.width)
|
|
|
|
|
let reducedAngle = Utility.mod(angle, range: 360, minMax: (0, 360))
|
|
|
|
|
let fromAngle = Conversion.degreesToRadians(CGFloat(-startAngle))
|
|
|
|
|
let toAngle = Conversion.degreesToRadians(CGFloat((clockwise == true ? -reducedAngle : reducedAngle) - startAngle))
|
|
|
|
|
|
|
|
|
|
CGContextAddArc(imageCtx, width/2.0, height/2.0, arcRadius, fromAngle, toAngle, clockwise == true ? 1 : 0)
|
|
|
|
|
|
|
|
|
|
let glowValue = GlowConstants.glowAmountForAngle(reducedAngle, glowAmount: glowAmount, glowMode: glowMode, size: width)
|
|
|
|
|
if glowValue > 0 {
|
|
|
|
|
CGContextSetShadowWithColor(imageCtx, CGSizeZero, glowValue, UIColor.blackColor().CGColor)
|
|
|
|
|
}
|
|
|
|
@@ -453,47 +463,45 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
//Gradient - Fill
|
|
|
|
|
if !lerpColorMode && colorsArray.count > 1 {
|
|
|
|
|
var componentsArray: [CGFloat] = []
|
|
|
|
|
let rgbColorsArray: [UIColor] = colorsArray.map {c in // Make sure every color in colors array is in RGB color space
|
|
|
|
|
if CGColorGetNumberOfComponents(c.CGColor) == 2 {
|
|
|
|
|
let whiteValue = CGColorGetComponents(c.CGColor)[0]
|
|
|
|
|
let rgbColorsArray: [UIColor] = colorsArray.map { color in // Make sure every color in colors array is in RGB color space
|
|
|
|
|
if CGColorGetNumberOfComponents(color.CGColor) == 2 {
|
|
|
|
|
let whiteValue = CGColorGetComponents(color.CGColor)[0]
|
|
|
|
|
return UIColor(red: whiteValue, green: whiteValue, blue: whiteValue, alpha: 1.0)
|
|
|
|
|
} else {
|
|
|
|
|
return c
|
|
|
|
|
return color
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for color in rgbColorsArray {
|
|
|
|
|
let colorComponents: UnsafePointer<CGFloat> = CGColorGetComponents(color.CGColor)
|
|
|
|
|
componentsArray.appendContentsOf([colorComponents[0],colorComponents[1],colorComponents[2],1.0])
|
|
|
|
|
let componentsArray = rgbColorsArray.flatMap { color -> [CGFloat] in
|
|
|
|
|
let components: UnsafePointer<CGFloat> = CGColorGetComponents(color.CGColor)
|
|
|
|
|
return [components[0], components[1], components[2], 1.0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drawGradientWithContext(ctx, componentsArray: componentsArray)
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
var color: UIColor! = nil
|
|
|
|
|
if colorsArray.count == 0 {
|
|
|
|
|
var color: UIColor?
|
|
|
|
|
if colorsArray.isEmpty {
|
|
|
|
|
color = UIColor.whiteColor()
|
|
|
|
|
} else if colorsArray.count == 1 {
|
|
|
|
|
color = colorsArray[0]
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// lerpColorMode is true
|
|
|
|
|
|
|
|
|
|
let t = CGFloat(reducedAngle) / 360
|
|
|
|
|
let steps = colorsArray.count - 1;
|
|
|
|
|
let step = 1 / CGFloat(steps);
|
|
|
|
|
let steps = colorsArray.count - 1
|
|
|
|
|
let step = 1 / CGFloat(steps)
|
|
|
|
|
for i in 1...steps {
|
|
|
|
|
let fi = CGFloat(i)
|
|
|
|
|
if (t <= fi * step || i == steps) {
|
|
|
|
|
let colorT = UtilityFunctions.InverseLerp(t, minMax: ((fi - 1) * step, fi * step))
|
|
|
|
|
color = UtilityFunctions.ColorLerp(colorT, minMax: (colorsArray[i - 1], colorsArray[i]));
|
|
|
|
|
break;
|
|
|
|
|
let colorT = Utility.inverseLerp(t, minMax: ((fi - 1) * step, fi * step))
|
|
|
|
|
color = Utility.colorLerp(colorT, minMax: (colorsArray[i - 1], colorsArray[i]))
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fillRectWithContext(ctx, color: color)
|
|
|
|
|
if let color = color {
|
|
|
|
|
fillRectWithContext(ctx, color: color)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CGContextRestoreGState(ctx)
|
|
|
|
|
UIGraphicsPopContext()
|
|
|
|
@@ -506,21 +514,24 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
|
|
|
|
|
private func drawGradientWithContext(ctx: CGContext!, componentsArray: [CGFloat]) {
|
|
|
|
|
let baseSpace = CGColorSpaceCreateDeviceRGB()
|
|
|
|
|
let locations = locationsCache ?? gradientLocationsFromColorCount(componentsArray.count/4, gradientWidth: bounds.size.width)
|
|
|
|
|
let locations = locationsCache ?? gradientLocationsForColorCount(componentsArray.count/4, gradientWidth: bounds.size.width)
|
|
|
|
|
let gradient: CGGradient
|
|
|
|
|
|
|
|
|
|
if let g = self.gradientCache {
|
|
|
|
|
gradient = g
|
|
|
|
|
if let cachedGradient = gradientCache {
|
|
|
|
|
gradient = cachedGradient
|
|
|
|
|
} else {
|
|
|
|
|
guard let g = CGGradientCreateWithColorComponents(baseSpace, componentsArray, locations,componentsArray.count / 4) else { return }
|
|
|
|
|
self.gradientCache = g
|
|
|
|
|
gradient = g
|
|
|
|
|
guard let cachedGradient = CGGradientCreateWithColorComponents(baseSpace, componentsArray, locations,componentsArray.count / 4) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gradientCache = cachedGradient
|
|
|
|
|
gradient = cachedGradient
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let halfX = bounds.size.width/2.0
|
|
|
|
|
let halfX = bounds.size.width / 2.0
|
|
|
|
|
let floatPi = CGFloat(M_PI)
|
|
|
|
|
let rotateSpeed = clockwise == true ? gradientRotateSpeed : gradientRotateSpeed * -1
|
|
|
|
|
let angleInRadians = ConversionFunctions.DegreesToRadians(rotateSpeed * CGFloat(angle) - 90)
|
|
|
|
|
let angleInRadians = Conversion.degreesToRadians(rotateSpeed * CGFloat(angle) - 90)
|
|
|
|
|
let oppositeAngle = angleInRadians > floatPi ? angleInRadians - floatPi : angleInRadians + floatPi
|
|
|
|
|
|
|
|
|
|
let startPoint = CGPoint(x: (cos(angleInRadians) * halfX) + halfX, y: (sin(angleInRadians) * halfX) + halfX)
|
|
|
|
@@ -529,19 +540,15 @@ public class KDCircularProgress: UIView {
|
|
|
|
|
CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, .DrawsBeforeStartLocation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func gradientLocationsFromColorCount(colorCount: Int, gradientWidth: CGFloat) -> [CGFloat] {
|
|
|
|
|
private func gradientLocationsForColorCount(colorCount: Int, gradientWidth: CGFloat) -> [CGFloat] {
|
|
|
|
|
if colorCount == 0 || gradientWidth == 0 {
|
|
|
|
|
return []
|
|
|
|
|
} else {
|
|
|
|
|
var locationsArray: [CGFloat] = []
|
|
|
|
|
let progressLineWidth = radius * progressThickness
|
|
|
|
|
let firstPoint = gradientWidth/2 - (radius - progressLineWidth/2)
|
|
|
|
|
let increment = (gradientWidth - (2*firstPoint))/CGFloat(colorCount - 1)
|
|
|
|
|
|
|
|
|
|
for i in 0..<colorCount {
|
|
|
|
|
locationsArray.append(firstPoint + (CGFloat(i) * increment))
|
|
|
|
|
}
|
|
|
|
|
assert(locationsArray.count == colorCount, "color counts should be equal")
|
|
|
|
|
let locationsArray = (0..<colorCount).map { firstPoint + (CGFloat($0) * increment) }
|
|
|
|
|
let result = locationsArray.map { $0 / gradientWidth }
|
|
|
|
|
locationsCache = result
|
|
|
|
|
return result
|
|
|
|
|