Library / Pickers, sliders & steppers
Picker wheel ticks
A custom wheel or scrubber that ticks once per row as it spins past values.
import SwiftUI
struct MinutePicker: View {
@State private var minutes = 5
var body: some View {
Picker("Minutes", selection: $minutes) {
ForEach(0..<60, id: \.self) { Text("\($0) min") }
}
.pickerStyle(.wheel)
// System wheels tick on-device already; this pattern is for
// custom scrubbers bound to any changing value:
.sensoryFeedback(.selection, trigger: minutes)
}
} final class Scrubber: UIScrollView, UIScrollViewDelegate {
private let tick = UISelectionFeedbackGenerator()
private var lastIndex = 0
private let rowHeight: CGFloat = 36
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let index = Int(round(scrollView.contentOffset.y / rowHeight))
if index != lastIndex {
lastIndex = index
tick.selectionChanged()
tick.prepare() // stay armed for fast flicks
}
}
}