Last active
June 4, 2023 21:54
-
-
Save KrisRJack/ff9c30d43aaacaa8ff72a5f32202c40c to your computer and use it in GitHub Desktop.
Haptic feedback in Swift
This file contains 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
// | |
// Vibration.swift | |
// Created by Kristopher Jackson. | |
// | |
import UIKit | |
import AudioToolbox | |
enum Vibration { | |
case error | |
case success | |
case warning | |
case light | |
case medium | |
case heavy | |
case soft | |
case rigid | |
case selection | |
case oldSchool | |
static func vibrate(with type: Vibration) { | |
switch type { | |
case .error: | |
UINotificationFeedbackGenerator().notificationOccurred(.error) | |
case .success: | |
UINotificationFeedbackGenerator().notificationOccurred(.success) | |
case .warning: | |
UINotificationFeedbackGenerator().notificationOccurred(.warning) | |
case .light: | |
UIImpactFeedbackGenerator(style: .light).impactOccurred() | |
case .medium: | |
UIImpactFeedbackGenerator(style: .medium).impactOccurred() | |
case .heavy: | |
UIImpactFeedbackGenerator(style: .heavy).impactOccurred() | |
case .soft: | |
UIImpactFeedbackGenerator(style: .soft).impactOccurred() | |
case .rigid: | |
UIImpactFeedbackGenerator(style: .rigid).impactOccurred() | |
case .selection: | |
UISelectionFeedbackGenerator().selectionChanged() | |
case .oldSchool: | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment