Skip to content

Instantly share code, notes, and snippets.

@YanSte
Last active August 18, 2022 08:02
Show Gist options
  • Save YanSte/9731f8cb2f9dcfec12e65682570b30c2 to your computer and use it in GitHub Desktop.
Save YanSte/9731f8cb2f9dcfec12e65682570b30c2 to your computer and use it in GitHub Desktop.
Vibrations, notification feedback type
import AVFoundation
import UIKit
// MARK: - Vibrations
public enum Vibrations {
/// A notification feedback type, indicating that a task has failed.
case error
/// A notification feedback type, indicating that a task has completed successfully.
case success
/// A notification feedback type, indicating that a task has produced a warning.
case warning
/// A notification feedback type, light.
case light
/// A notification feedback type, medium.
case medium
/// A notification feedback type, heavy.
case heavy
/// A notification feedback type, selection.
case selection
case oldSchool
public func vibrate() {
switch self {
case .error:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
case .success:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
case .warning:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
case .light:
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
case .medium:
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
case .heavy:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
case .selection:
let generator = UISelectionFeedbackGenerator()
generator.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