diff --git a/Pod/Classes/GaugeLayer.swift b/Pod/Classes/GaugeLayer.swift index 1d29978..c0b2895 100644 --- a/Pod/Classes/GaugeLayer.swift +++ b/Pod/Classes/GaugeLayer.swift @@ -61,24 +61,21 @@ class GaugeLayer: CALayer { /// //MARK: - Init methods - override init(layer: Any) { - super.init(layer: layer) - - if ((layer as! AnyObject).isKind(of: GaugeLayer.self)) { - if let previous = layer as? GaugeLayer { - startAngle = previous.startAngle - stopAngle = previous.stopAngle - - radius = previous.radius - thickness = previous.thickness - gaugeBackgroundColor = previous.gaugeBackgroundColor - gaugeColor = previous.gaugeColor - } + override init(layer: Any) { + super.init(layer: layer) + if ((layer as AnyObject).isKind(of: GaugeLayer.self)) { + if let previous = layer as? GaugeLayer { + startAngle = previous.startAngle + stopAngle = previous.stopAngle + radius = previous.radius + thickness = previous.thickness + gaugeBackgroundColor = previous.gaugeBackgroundColor + gaugeColor = previous.gaugeColor + } + } + setup() } - - setup() - } - + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } @@ -132,7 +129,7 @@ class GaugeLayer: CALayer { ctx.setShouldAntialias(true) - ctx.addArc(center: CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2), radius: radius-(thickness/2), startAngle: 0, endAngle: CGFloat(2.0*M_PI), clockwise: false) + ctx.addArc(center: CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2), radius: radius-(thickness/2), startAngle: 0, endAngle: CGFloat(2.0*Double.pi), clockwise: false) ctx.setLineWidth(thickness) ctx.setLineCap(.round) ctx.setStrokeColor(gaugeBackgroundColor.cgColor) @@ -148,7 +145,7 @@ class GaugeLayer: CALayer { //MARK: - Class helper methods private func convertDegreesToRadius(degrees: CGFloat) -> CGFloat { - return ((CGFloat(M_PI) * degrees) / 180.0) + return ((CGFloat(Double.pi) * degrees) / 180.0) } } diff --git a/Pod/Classes/GaugeView.swift b/Pod/Classes/GaugeView.swift index 87d08d9..cdc5544 100644 --- a/Pod/Classes/GaugeView.swift +++ b/Pod/Classes/GaugeView.swift @@ -30,9 +30,9 @@ public class GaugeView: UIView { // Class proprty /// - private var label: UILabel! + private var label: UILabel? - private var gaugeLayer: GaugeLayer! + private var gaugeLayer: GaugeLayer? //Gauge property @IBInspectable public var startAngle: Float = 0.0 @@ -82,7 +82,7 @@ public class GaugeView: UIView { //Label property @IBInspectable public var labelText: String = "" { didSet { - label.text = labelText + label?.text = labelText updateTextLabel() } } @@ -90,7 +90,7 @@ public class GaugeView: UIView { @IBInspectable public var labelFont: UIFont? { didSet { if let labelFont = labelFont { - label.font = labelFont + label?.font = labelFont updateTextLabel() } } @@ -99,7 +99,7 @@ public class GaugeView: UIView { @IBInspectable public var labelColor: UIColor? { didSet { if let labelColor = labelColor { - label.textColor = labelColor + label?.textColor = labelColor updateTextLabel() } } @@ -130,14 +130,14 @@ public class GaugeView: UIView { override public func draw(_ rect: CGRect) { super.draw(rect) - gaugeLayer.radius = radius - gaugeLayer.thickness = thickness - gaugeLayer.frame = self.bounds - gaugeLayer.gaugeBackgroundColor = gaugeBackgroundColor - gaugeLayer.gaugeColor = gaugeColor - gaugeLayer.animationDuration = animationDuration - gaugeLayer.startAngle = convertDegreesToRadius(degrees: startAngle) - gaugeLayer.stopAngle = convertPercentageInRadius(percentage: percentage) + gaugeLayer?.radius = radius + gaugeLayer?.thickness = thickness + gaugeLayer?.frame = self.bounds + gaugeLayer?.gaugeBackgroundColor = gaugeBackgroundColor + gaugeLayer?.gaugeColor = gaugeColor + gaugeLayer?.animationDuration = animationDuration + gaugeLayer?.startAngle = convertDegreesToRadius(degrees: startAngle) + gaugeLayer?.stopAngle = convertPercentageInRadius(percentage: percentage) updateTextLabel() } @@ -157,16 +157,16 @@ public class GaugeView: UIView { private func createGaugeView() { gaugeLayer = GaugeLayer(layer: layer) - gaugeLayer.radius = radius - gaugeLayer.thickness = thickness - gaugeLayer.frame = self.bounds - gaugeLayer.gaugeBackgroundColor = gaugeBackgroundColor - gaugeLayer.gaugeColor = gaugeColor - gaugeLayer.animationDuration = animationDuration - gaugeLayer.startAngle = convertDegreesToRadius(degrees: startAngle) - gaugeLayer.stopAngle = convertPercentageInRadius(percentage: percentage) + gaugeLayer?.radius = radius + gaugeLayer?.thickness = thickness + gaugeLayer?.frame = self.bounds + gaugeLayer?.gaugeBackgroundColor = gaugeBackgroundColor + gaugeLayer?.gaugeColor = gaugeColor + gaugeLayer?.animationDuration = animationDuration + gaugeLayer?.startAngle = convertDegreesToRadius(degrees: startAngle) + gaugeLayer?.stopAngle = convertPercentageInRadius(percentage: percentage) - layer.addSublayer(gaugeLayer) + layer.addSublayer(gaugeLayer!) self.backgroundColor = UIColor.clear } @@ -176,12 +176,12 @@ public class GaugeView: UIView { updateTextLabel() - self.addSubview(label) + self.addSubview(label!) } private func updateTextLabel() { - label.sizeToFit() - label.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2) + label?.sizeToFit() + label?.center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2) } //MARK: - Utility method @@ -190,7 +190,7 @@ public class GaugeView: UIView { } private func convertDegreesToRadius(degrees: Float) -> Float { - return ((Float(M_PI) * degrees) / 180.0) + return ((Float(Double.pi) * degrees) / 180.0) } }