Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simplified way to call haptic feedback
import Foundation
enum HapticStyle {
case light
case medium
case heavy
case success
case error
case warning
case selection
}
func hapticFeedbackWithStyle(_ style: HapticStyle) {
if #available(iOS 10.0, *) {
if style == .light {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
}
if style == .medium {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}
if style == .heavy {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
}
if style == .selection {
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
}
if style == .error {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
}
if style == .warning {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
}
if style == .success {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment