Skip to content

Instantly share code, notes, and snippets.

@jonahaung
Created November 28, 2019 17:46
Show Gist options
  • Save jonahaung/191379b30d7952edca598633de26066c to your computer and use it in GitHub Desktop.
Save jonahaung/191379b30d7952edca598633de26066c to your computer and use it in GitHub Desktop.
import UIKit
import AudioToolbox
enum AlertTones: SystemSoundID {
case MailSent = 1001
case MailReceived = 1000
case receivedMessage = 1003
case sendMessage = 1004
case Tock = 1105
case Typing = 1305
}
enum Vibration {
case error
case success
case warning
case light
case medium
case heavy
case selection
case oldSchool
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))
}
}
}
final class SoundManager {
class func playSound(tone: AlertTones) {
AudioServicesPlaySystemSound(tone.rawValue)
}
class func tock() {
SoundManager.playSound(tone: .Tock)
}
class func vibrate(vibration: Vibration) {
vibration.vibrate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment