Last active
July 11, 2023 10:42
-
-
Save Harry-Harrison/e4217a6d8c4cfbee1aa5128c4491a149 to your computer and use it in GitHub Desktop.
Haptic Feedback Vibrations in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 is great. works on me
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
This is great! 👍