Haptic Feedback Vibrations in SwiftUI
// This prints a list of buttons that on tap will fire a different type of haptic vibration | |
import SwiftUI | |
struct ContentView: View { | |
let generator = UINotificationFeedbackGenerator() | |
var body: some View { | |
VStack(alignment: .center, spacing: 30.0) { | |
Button(action: { | |
self.generator.notificationOccurred(.success) | |
}) { | |
Text("Notification - Success") | |
} | |
Button(action: { | |
self.generator.notificationOccurred(.error) | |
}) { | |
Text("Notification - Error") | |
} | |
Button(action: { | |
self.generator.notificationOccurred(.warning) | |
}) { | |
Text("Notification - Warning") | |
} | |
Button(action: { | |
let impactLight = UIImpactFeedbackGenerator(style: .light) | |
impactLight.impactOccurred() | |
}) { | |
Text("Impact - Light") | |
} | |
Button(action: { | |
let impactMed = UIImpactFeedbackGenerator(style: .medium) | |
impactMed.impactOccurred() | |
}) { | |
Text("Impact - Medium") | |
} | |
Button(action: { | |
let impactHeavy = UIImpactFeedbackGenerator(style: .heavy) | |
impactHeavy.impactOccurred() | |
}) { | |
Text("Impact - Heavy") | |
} | |
Button(action: { | |
let selectionFeedback = UISelectionFeedbackGenerator() | |
selectionFeedback.selectionChanged() | |
}) { | |
Text("Selection Feedback - Changed") | |
} | |
} | |
.padding(.all, 30.0) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
@USBA Good catch, fixed in the original |
This comment has been minimized.
This comment has been minimized.
This is great! |
This comment has been minimized.
This comment has been minimized.
This is great. works on me |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
To get "warning" haptic feedback, replace
.error
with. warning