Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions Pod/Classes/GaugeLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

}
52 changes: 26 additions & 26 deletions Pod/Classes/GaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,15 +82,15 @@ public class GaugeView: UIView {
//Label property
@IBInspectable public var labelText: String = "" {
didSet {
label.text = labelText
label?.text = labelText
updateTextLabel()
}
}

@IBInspectable public var labelFont: UIFont? {
didSet {
if let labelFont = labelFont {
label.font = labelFont
label?.font = labelFont
updateTextLabel()
}
}
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}

}