Skip to content

Commit 38ea2fd

Browse files
author
Rayan Saeed
committed
Updated for Swift 3
1 parent 0dda408 commit 38ea2fd

1 file changed

Lines changed: 73 additions & 73 deletions

File tree

Pod/Classes/SVGPlayButton.swift

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ private let darkGray = UIColor(
2323

2424
private let kInnerRadiusScaleFactor = CGFloat(0.05)
2525

26-
@IBDesignable public class SVGPlayButton: UIButton {
26+
@IBDesignable open class SVGPlayButton: UIButton {
2727

28-
@IBInspectable public var playing: Bool = false {
28+
@IBInspectable open var playing: Bool = false {
2929
didSet {
3030
if playing {
3131
presentForPlaying()
@@ -35,30 +35,30 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
3535
}
3636
}
3737

38-
private var progressTrackShapeLayer: CAShapeLayer = CAShapeLayer()
38+
fileprivate var progressTrackShapeLayer: CAShapeLayer = CAShapeLayer()
3939

40-
private var progressShapeLayer: CAShapeLayer = CAShapeLayer()
40+
fileprivate var progressShapeLayer: CAShapeLayer = CAShapeLayer()
4141

42-
private var playShapeLayer: CAShapeLayer = CAShapeLayer()
42+
fileprivate var playShapeLayer: CAShapeLayer = CAShapeLayer()
4343

44-
private var pauseShapeLayerLeft: CAShapeLayer = CAShapeLayer()
44+
fileprivate var pauseShapeLayerLeft: CAShapeLayer = CAShapeLayer()
4545

46-
private var pauseShapeLayerRight: CAShapeLayer = CAShapeLayer()
46+
fileprivate var pauseShapeLayerRight: CAShapeLayer = CAShapeLayer()
4747

48-
@IBInspectable public var progressColor: UIColor = salmonColor
48+
@IBInspectable open var progressColor: UIColor = salmonColor
4949

50-
@IBInspectable public var progressTrackColor: UIColor = lightGray
50+
@IBInspectable open var progressTrackColor: UIColor = lightGray
5151

52-
@IBInspectable public var playColor: UIColor = darkGray
52+
@IBInspectable open var playColor: UIColor = darkGray
5353

54-
@IBInspectable public var pauseColor: UIColor = darkGray
54+
@IBInspectable open var pauseColor: UIColor = darkGray
5555

5656
//
5757
// If actions are not disabled, the progress layer's strokeEnd update will animate by default. Because we update this so many times a second, like 60
5858
// times a second, there will be a noticeable lag in the view's representation of the path w/r/t where the current strokeEnd actually 'is'. Turning off animations
5959
// solves this b/c the path updates immediately, and since we're updating at such a high number of times per second, it looks smooth when one's looking watching the view.
6060
//
61-
@IBInspectable public var progressStrokeEnd: CGFloat = 0 {
61+
@IBInspectable open var progressStrokeEnd: CGFloat = 0 {
6262
didSet {
6363
if progressStrokeEnd < 0 || progressStrokeEnd > 1 {
6464
self.resetProgressLayer()
@@ -70,9 +70,9 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
7070
}
7171
}
7272

73-
public var willPlay: (() -> ())?
73+
open var willPlay: (() -> ())?
7474

75-
public var willPause: (() -> ())?
75+
open var willPause: (() -> ())?
7676

7777
required public init?(coder aDecoder: NSCoder) {
7878
super.init(coder: aDecoder)
@@ -84,36 +84,36 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
8484
sharedInit()
8585
}
8686

87-
private func sharedInit() {
88-
self.addTarget(self, action: "touchUpInsideHandler", forControlEvents: UIControlEvents.TouchUpInside)
87+
fileprivate func sharedInit() {
88+
self.addTarget(self, action: #selector(SVGPlayButton.touchUpInsideHandler), for: UIControlEvents.touchUpInside)
8989
}
90-
91-
override public func drawRect(rect: CGRect) {
92-
90+
91+
override open func draw(_ rect: CGRect) {
92+
9393
//
9494
// Pause
9595
//
9696

97-
let center: CGPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))
98-
let pauseLineHeight = CGRectGetHeight(rect) * 0.357
99-
let pauseLineWidth = CGRectGetWidth(rect) * 0.0714
97+
let center: CGPoint = CGPoint(x: rect.midX, y: rect.midY)
98+
let pauseLineHeight = rect.height * 0.357
99+
let pauseLineWidth = rect.width * 0.0714
100100

101101
enum PauseLinePosition {
102-
case Left
103-
case Right
102+
case left
103+
case right
104104
}
105105

106-
func pauseLine(position: PauseLinePosition) -> UIBezierPath {
106+
func pauseLine(_ position: PauseLinePosition) -> UIBezierPath {
107107

108108
let pauseLineRectY = (bounds.height/2) - (pauseLineHeight * 0.5)
109-
var pauseLineRect = CGRectMake(0, pauseLineRectY, pauseLineWidth, pauseLineHeight)
109+
var pauseLineRect = CGRect(x: 0, y: pauseLineRectY, width: pauseLineWidth, height: pauseLineHeight)
110110

111-
if position == .Left {
111+
if position == .left {
112112
let pauseLineRectX = (bounds.width / 2) - (pauseLineWidth * 0.5) - (pauseLineWidth * 1.25)
113113
pauseLineRect.origin.x = pauseLineRectX
114114
}
115115

116-
if position == .Right {
116+
if position == .right {
117117
let pauseLineRectX = (bounds.width / 2) - (pauseLineWidth * 0.5) + (pauseLineWidth * 1.25)
118118
pauseLineRect.origin.x = pauseLineRectX
119119
}
@@ -123,44 +123,44 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
123123
return pauseLinePath
124124
}
125125

126-
pauseShapeLayerLeft.path = pauseLine(.Left).CGPath
127-
pauseShapeLayerLeft.fillColor = pauseColor.CGColor
128-
pauseShapeLayerLeft.hidden = self.playing ? false : true
126+
pauseShapeLayerLeft.path = pauseLine(.left).cgPath
127+
pauseShapeLayerLeft.fillColor = pauseColor.cgColor
128+
pauseShapeLayerLeft.isHidden = self.playing ? false : true
129129
self.layer.addSublayer(pauseShapeLayerLeft)
130130

131-
pauseShapeLayerRight.path = pauseLine(.Right).CGPath
132-
pauseShapeLayerRight.fillColor = pauseColor.CGColor
133-
pauseShapeLayerRight.hidden = self.playing ? false : true
131+
pauseShapeLayerRight.path = pauseLine(.right).cgPath
132+
pauseShapeLayerRight.fillColor = pauseColor.cgColor
133+
pauseShapeLayerRight.isHidden = self.playing ? false : true
134134
self.layer.addSublayer(pauseShapeLayerRight)
135135

136136
//
137137
// Play
138138
//
139139

140-
let midY = CGRectGetMidY(rect)
141-
let playLeftX = CGRectGetWidth(rect) * 0.4107
140+
let midY = rect.midY
141+
let playLeftX = rect.width * 0.4107
142142

143143
let playPath = UIBezierPath()
144-
playPath.lineJoinStyle = CGLineJoin.Round
145-
playPath.moveToPoint(CGPointMake(playLeftX, midY))
146-
playPath.addLineToPoint(CGPointMake(playLeftX, (midY - CGRectGetHeight(rect) * 0.17)))
147-
playPath.addLineToPoint(CGPointMake(playLeftX + (CGRectGetWidth(rect) * 0.2322), midY))
148-
playPath.addLineToPoint(CGPointMake(playLeftX, (midY + CGRectGetHeight(rect) * 0.17)))
149-
playPath.addLineToPoint(CGPointMake(playLeftX, midY))
150-
151-
playShapeLayer.path = playPath.CGPath
152-
playShapeLayer.strokeColor = playColor.CGColor
153-
playShapeLayer.fillColor = playColor.CGColor
154-
playShapeLayer.hidden = self.playing ? true : false
144+
playPath.lineJoinStyle = CGLineJoin.round
145+
playPath.move(to: CGPoint(x: playLeftX, y: midY))
146+
playPath.addLine(to: CGPoint(x: playLeftX, y: (midY - rect.height * 0.17)))
147+
playPath.addLine(to: CGPoint(x: playLeftX + (rect.width * 0.2322), y: midY))
148+
playPath.addLine(to: CGPoint(x: playLeftX, y: (midY + rect.height * 0.17)))
149+
playPath.addLine(to: CGPoint(x: playLeftX, y: midY))
150+
151+
playShapeLayer.path = playPath.cgPath
152+
playShapeLayer.strokeColor = playColor.cgColor
153+
playShapeLayer.fillColor = playColor.cgColor
154+
playShapeLayer.isHidden = self.playing ? true : false
155155
self.layer.addSublayer(playShapeLayer)
156156

157157
// helper
158-
func d2R(degrees: CGFloat) -> CGFloat {
158+
func d2R(_ degrees: CGFloat) -> CGFloat {
159159
return degrees * 0.0174532925 // 1 degree ~ 0.0174532925 radians
160160
}
161161

162-
let arcWidth = (CGRectGetWidth(rect) * kInnerRadiusScaleFactor)
163-
let radius = (CGRectGetMidY(rect) - arcWidth/2)
162+
let arcWidth = (rect.width * kInnerRadiusScaleFactor)
163+
let radius = (rect.midY - arcWidth/2)
164164

165165
//
166166
// Progress 'track'
@@ -172,19 +172,19 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
172172
//
173173

174174
/*
175-
let progressTrackPath = UIBezierPath(arcCenter: CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)), radius: radius, startAngle: degrees2Radians(270), endAngle: degrees2Radians(269.99), clockwise: true)
176-
progressTrackPath.lineWidth = arcWidth
177-
kDefaultProgressTrackColor.setStroke()
178-
progressTrackPath.stroke()
179-
*/
175+
let progressTrackPath = UIBezierPath(arcCenter: CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)), radius: radius, startAngle: degrees2Radians(270), endAngle: degrees2Radians(269.99), clockwise: true)
176+
progressTrackPath.lineWidth = arcWidth
177+
kDefaultProgressTrackColor.setStroke()
178+
progressTrackPath.stroke()
179+
*/
180180

181181
func progressArc() -> CGPath {
182-
return UIBezierPath(arcCenter: CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)), radius: radius, startAngle: d2R(270), endAngle: d2R(269.99), clockwise: true).CGPath
182+
return UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.midY), radius: radius, startAngle: d2R(270), endAngle: d2R(269.99), clockwise: true).cgPath
183183
}
184184

185185
progressTrackShapeLayer.path = progressArc()
186-
progressTrackShapeLayer.strokeColor = progressTrackColor.CGColor
187-
progressTrackShapeLayer.fillColor = UIColor.clearColor().CGColor
186+
progressTrackShapeLayer.strokeColor = progressTrackColor.cgColor
187+
progressTrackShapeLayer.fillColor = UIColor.clear.cgColor
188188
progressTrackShapeLayer.lineWidth = arcWidth
189189
self.layer.addSublayer(progressTrackShapeLayer)
190190

@@ -193,16 +193,16 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
193193
//
194194

195195
progressShapeLayer.path = progressArc()
196-
progressShapeLayer.strokeColor = progressColor.CGColor
197-
progressShapeLayer.fillColor = UIColor.clearColor().CGColor
196+
progressShapeLayer.strokeColor = progressColor.cgColor
197+
progressShapeLayer.fillColor = UIColor.clear.cgColor
198198
progressShapeLayer.lineWidth = arcWidth
199199
progressShapeLayer.strokeStart = 0
200200
progressShapeLayer.strokeEnd = self.progressStrokeEnd
201201
self.layer.addSublayer(progressShapeLayer)
202202

203203
}
204204

205-
public func resetProgressLayer() {
205+
open func resetProgressLayer() {
206206
self.progressStrokeEnd = 0
207207
}
208208

@@ -220,25 +220,25 @@ private let kInnerRadiusScaleFactor = CGFloat(0.05)
220220
}
221221
}
222222

223-
private func presentForPlaying() {
224-
playShapeLayer.hidden = true
225-
pauseShapeLayerLeft.hidden = false
226-
pauseShapeLayerRight.hidden = false
223+
fileprivate func presentForPlaying() {
224+
playShapeLayer.isHidden = true
225+
pauseShapeLayerLeft.isHidden = false
226+
pauseShapeLayerRight.isHidden = false
227227
self.animate()
228228
}
229229

230-
private func presentForPaused() {
231-
playShapeLayer.hidden = false
232-
pauseShapeLayerLeft.hidden = true
233-
pauseShapeLayerRight.hidden = true
230+
fileprivate func presentForPaused() {
231+
playShapeLayer.isHidden = false
232+
pauseShapeLayerLeft.isHidden = true
233+
pauseShapeLayerRight.isHidden = true
234234
self.animate()
235235
}
236236

237-
private func animate() {
238-
let t1 = CGAffineTransformMakeScale(0.8, 0.8)
237+
fileprivate func animate() {
238+
let t1 = CGAffineTransform(scaleX: 0.8, y: 0.8)
239239
self.transform = t1
240-
UIView.animateWithDuration(0.75, delay: 0, usingSpringWithDamping: 0.225, initialSpringVelocity: 0.7, options: .BeginFromCurrentState, animations: { () -> Void in
241-
let t2 = CGAffineTransformMakeScale(1.0, 1.0)
240+
UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 0.225, initialSpringVelocity: 0.7, options: .beginFromCurrentState, animations: { () -> Void in
241+
let t2 = CGAffineTransform(scaleX: 1.0, y: 1.0)
242242
self.transform = t2
243243
}, completion: { (b) -> Void in
244244
//

0 commit comments

Comments
 (0)