Library / Pickers, sliders & steppers
Stepper increase / decrease
Quantity steppers (+ / −): direction-aware feedback so up and down feel different.
import SwiftUI
struct QuantityStepper: View {
@State private var quantity = 1
@State private var limitBump = 0
let range = 1...9
var body: some View {
HStack(spacing: 20) {
Button { decrement() } label: { Image(systemName: "minus.circle.fill") }
Text("\(quantity)").font(.title2.monospacedDigit())
Button { increment() } label: { Image(systemName: "plus.circle.fill") }
}
.font(.title)
// Direction-aware ticks:
.sensoryFeedback(trigger: quantity) { old, new in
new > old ? .increase : .decrease
}
// Firm bump when pushing against a limit:
.sensoryFeedback(.impact(flexibility: .rigid, intensity: 0.9),
trigger: limitBump)
}
func increment() {
if quantity < range.upperBound { quantity += 1 } else { limitBump += 1 }
}
func decrement() {
if quantity > range.lowerBound { quantity -= 1 } else { limitBump += 1 }
}
}