Skip to content

Commit 63a8336

Browse files
committed
Add support for pointer interactions
1 parent 3ce52db commit 63a8336

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// TactileSlider+UIPointerInteractionDelegate.swift
3+
// TactileSlider
4+
//
5+
// Created by Dale Price on 5/1/20.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
@available(iOS 13.4, *)
12+
extension TactileSlider: UIPointerInteractionDelegate {
13+
14+
internal func setUpPointerInteraction() {
15+
let pointerInteraction = UIPointerInteraction(delegate: self)
16+
self.addInteraction(pointerInteraction)
17+
}
18+
19+
public func targetedPreview(for interaction: UIPointerInteraction) -> UITargetedPreview? {
20+
if let interactionView = interaction.view {
21+
let previewParameters = UIPreviewParameters()
22+
previewParameters.visiblePath = UIBezierPath(roundedRect: bounds, cornerRadius: automaticCornerRadius)
23+
previewParameters.backgroundColor = .clear
24+
25+
return UITargetedPreview(view: interactionView, parameters: previewParameters)
26+
}
27+
28+
return nil
29+
}
30+
31+
/// override this to set a custom pointer interaction style
32+
open func pointerStyle(with targetedPreview: UITargetedPreview) -> UIPointerStyle {
33+
return UIPointerStyle(
34+
effect: UIPointerEffect.hover(targetedPreview, preferredTintMode: UIPointerEffect.TintMode.overlay, prefersShadow: false, prefersScaledContent: true)
35+
)
36+
}
37+
38+
open func pointerInteraction(_ interaction: UIPointerInteraction, regionFor request: UIPointerRegionRequest, defaultRegion: UIPointerRegion) -> UIPointerRegion? {
39+
return UIPointerRegion(rect: self.bounds)
40+
}
41+
42+
open func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
43+
var style: UIPointerStyle? = nil
44+
45+
if isPointerInteractionEnabled {
46+
if let targetedPreview = targetedPreview(for: interaction) {
47+
style = pointerStyle(with: targetedPreview)
48+
}
49+
}
50+
51+
return style
52+
}
53+
54+
}

TactileSlider/Classes/TactileSlider.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ import UIKit
116116
}
117117
}
118118

119+
/// If true, the slider will use a hover effect as the pointer hovers over it.
120+
///
121+
/// Override the `pointerStyle(with:)` method to customize the effect.
122+
///
123+
/// - Requires: iOS 13.4
124+
@IBInspectable open var isPointerInteractionEnabled: Bool = true
125+
119126
/// If true, the slider will animate its scale when it is being dragged
127+
///
128+
/// - Warning: Not recommended together with `isPointerInteractionEnabled = true`
120129
@IBInspectable open var scaleUpWhenInUse: Bool = false
121130

122131
/// The color of the track the slider slides along
@@ -142,7 +151,7 @@ import UIKit
142151
@IBInspectable open var cornerRadius: CGFloat = -1 {
143152
didSet {
144153
if cornerRadius < 0 {
145-
renderer.cornerRadius = min(bounds.width, bounds.height) / 3.3
154+
renderer.cornerRadius = automaticCornerRadius
146155
} else {
147156
renderer.cornerRadius = cornerRadius
148157
}
@@ -176,6 +185,14 @@ import UIKit
176185
}
177186
}
178187

188+
internal var automaticCornerRadius: CGFloat {
189+
if cornerRadius < 0 {
190+
return min(bounds.width, bounds.height) / 3.3
191+
} else {
192+
return cornerRadius
193+
}
194+
}
195+
179196
private let renderer = TactileSliderLayerRenderer()
180197

181198
private var dragGestureRecognizer: UIPanGestureRecognizer!
@@ -240,10 +257,11 @@ import UIKit
240257
setTapEnabled()
241258
if #available(iOS 13.4, *) {
242259
setScrollingEnabled()
260+
setUpPointerInteraction()
243261
}
244262

245263
renderer.tactileSlider = self
246-
renderer.cornerRadius = cornerRadius
264+
renderer.cornerRadius = automaticCornerRadius
247265
traitCollectionDidChange(nil)
248266

249267
layer.backgroundColor = UIColor.clear.cgColor
@@ -405,7 +423,7 @@ import UIKit
405423

406424
private func updateLayerFrames() {
407425
if cornerRadius < 0 {
408-
renderer.cornerRadius = min(bounds.width, bounds.height) / 3.3
426+
renderer.cornerRadius = automaticCornerRadius
409427
}
410428
renderer.updateBounds(bounds)
411429
}

0 commit comments

Comments
 (0)