Last active
May 28, 2021 14:59
-
-
Save ralfebert/c246c1cfcb1178c68df237abd004cac0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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