Created
January 7, 2022 13:43
-
-
Save eugenezuban/d90a258ae239f6f9a070eb10b7f184a7 to your computer and use it in GitHub Desktop.
Haptic Feedback Vibrations in SwiftUI
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 SwiftUI | |
// MARK: - Haptic Feedback Vibrations | |
enum HapticType { | |
case light | |
case medium | |
case heavy | |
case success | |
case error | |
case warning | |
} | |
func getHapticFeedback(type: HapticType){ | |
let generator = UINotificationFeedbackGenerator() | |
switch type { | |
case .light: | |
let impactLight = UIImpactFeedbackGenerator(style: .light) | |
impactLight.impactOccurred() | |
case .medium: | |
let impactLight = UIImpactFeedbackGenerator(style: .medium) | |
impactLight.impactOccurred() | |
case .heavy: | |
let impactLight = UIImpactFeedbackGenerator(style: .heavy) | |
impactLight.impactOccurred() | |
case .success: | |
generator.notificationOccurred(.success) | |
case .error: | |
generator.notificationOccurred(.error) | |
case .warning: | |
generator.notificationOccurred(.warning) | |
} | |
} | |
struct HapticFeedbackVibrations: View { | |
var body: some View { | |
VStack{ | |
Button { | |
getHapticFeedback(type: .light) | |
} label: { | |
Text("Get vibration") | |
} //: BUTTON | |
} //: VSTACK | |
} | |
} | |
struct HapticFeedbackVibrations_Previews: PreviewProvider { | |
static var previews: some View { | |
HapticFeedbackVibrations() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment