Skip to content

Commit 9a5bac5

Browse files
committed
Add option to animate the slider's scale to give a visual indication during the pan gesture when adjusting its value
1 parent fa74be9 commit 9a5bac5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

TactileSlider/Classes/TactileSlider.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ import UIKit
5757
}
5858

5959
@IBInspectable open var isContinuous: Bool = true
60+
61+
// If true, a single tap anywhere in the slider will set it to that value
6062
@IBInspectable open var enableTapping: Bool = true
6163

64+
// If true, the slider will animate its scale when it is being dragged
65+
@IBInspectable open var scaleUpWhenInUse: Bool = false
66+
6267
@IBInspectable open var trackBackground: UIColor = UIColor.darkGray {
6368
didSet {
6469
renderer.trackBackground = trackBackground
@@ -205,6 +210,12 @@ import UIKit
205210
if isContinuous || sender.state == .ended || sender.state == .cancelled {
206211
sendActions(for: .valueChanged)
207212
}
213+
214+
if sender.state != .ended && sender.state != .cancelled {
215+
renderer.popUp = scaleUpWhenInUse
216+
} else {
217+
renderer.popUp = false
218+
}
208219
}
209220
}
210221

TactileSlider/Classes/TactileSliderLayerRenderer.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ internal class TactileSliderLayerRenderer {
3535
}
3636
}
3737

38+
var popUp: Bool = false {
39+
didSet(oldValue) {
40+
if oldValue != popUp {
41+
updatePopUp()
42+
}
43+
}
44+
}
45+
3846
let trackLayer = CALayer()
3947
let thumbLayer = CAShapeLayer()
4048
let maskLayer = CAShapeLayer()
@@ -73,6 +81,21 @@ internal class TactileSliderLayerRenderer {
7381
trackLayer.opacity = alpha
7482
}
7583

84+
private func updatePopUp() {
85+
CATransaction.begin()
86+
CATransaction.setDisableActions(true)
87+
88+
let zPosition: CGFloat = popUp ? 1.025 : 1
89+
trackLayer.transform = CATransform3DScale(CATransform3DIdentity, zPosition, zPosition, zPosition)
90+
91+
let animation = CABasicAnimation(keyPath: "transform.scale")
92+
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
93+
animation.duration = 0.1
94+
trackLayer.add(animation, forKey: nil)
95+
96+
CATransaction.commit()
97+
}
98+
7699
internal func updateBounds(_ bounds: CGRect) {
77100
CATransaction.begin()
78101
CATransaction.setDisableActions(true)

0 commit comments

Comments
 (0)