I\'m trying to implement haptic feedback at the beginning of a tap for a Button in SwiftUI. Therefore I\'m trying to use simultaneousGesture, but I\'m sill struggling. I can\'t
As I know, this is the most simplest way to get haptic feedback in SwiftUI
When tapping a button :
Button(action: {
let impactMed = UIImpactFeedbackGenerator(style: .medium)
impactMed.impactOccurred()
}) {
Text("This is a Button")
}
You can change the intensity of the haptic feedback by replacing .medium
with .soft
, .light
, .heavy
, or .rigid
or when tapping anything else :
.onTapGesture {
let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
impactHeavy.impactOccurred()
}
If you want to make something like Haptic Touch, replace .onTapGesture
with .onLongPressGesture
like this
.onLongPressGesture {
let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
impactHeavy.impactOccurred()
}