Skip to content

Instantly share code, notes, and snippets.

@eugenezuban
Created January 7, 2022 13:43
Show Gist options
  • Save eugenezuban/d90a258ae239f6f9a070eb10b7f184a7 to your computer and use it in GitHub Desktop.
Save eugenezuban/d90a258ae239f6f9a070eb10b7f184a7 to your computer and use it in GitHub Desktop.
Haptic Feedback Vibrations in SwiftUI
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