Skip to content

Instantly share code, notes, and snippets.

@budidino
Last active February 11, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budidino/47793605c3114a908608411ad5dcd0fe to your computer and use it in GitHub Desktop.
Save budidino/47793605c3114a908608411ad5dcd0fe to your computer and use it in GitHub Desktop.
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