Skip to content

Instantly share code, notes, and snippets.

@Harry-Harrison
Last active July 11, 2023 10:42
Show Gist options
  • Save Harry-Harrison/e4217a6d8c4cfbee1aa5128c4491a149 to your computer and use it in GitHub Desktop.
Save Harry-Harrison/e4217a6d8c4cfbee1aa5128c4491a149 to your computer and use it in GitHub Desktop.
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()
}
}
@djrobby
Copy link

djrobby commented Jun 4, 2020

This is great! 👍

@Nuhanabilah
Copy link

This is great. works on me

@EngOmarElsayed
Copy link

how can I test in a simulation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment