Skip to content

Instantly share code, notes, and snippets.

@ralfebert
Last active May 28, 2021 14:59
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 ralfebert/c246c1cfcb1178c68df237abd004cac0 to your computer and use it in GitHub Desktop.
Save ralfebert/c246c1cfcb1178c68df237abd004cac0 to your computer and use it in GitHub Desktop.
import CoreHaptics
import SwiftUI
enum FeedbackType {
case selection, success, error
}
class Haptics {
@AppStorage("HaptikFeedbackEnabled") var haptikFeedbackEnabled = true
lazy var notificationFeedbackGenerator = UINotificationFeedbackGenerator()
lazy var selectionFeedbackGenerator = UISelectionFeedbackGenerator()
func trigger(_ type: FeedbackType) {
if self.haptikFeedbackEnabled {
switch type {
case .selection:
self.selectionFeedbackGenerator.selectionChanged()
case .success:
self.notificationFeedbackGenerator.notificationOccurred(.success)
case .error:
self.notificationFeedbackGenerator.notificationOccurred(.error)
}
}
}
var isSupported: Bool {
let hapticCapability = CHHapticEngine.capabilitiesForHardware()
return hapticCapability.supportsHaptics
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment