Skip to content

Instantly share code, notes, and snippets.

@KrisRJack
Last active June 4, 2023 21:54
Show Gist options
  • Save KrisRJack/ff9c30d43aaacaa8ff72a5f32202c40c to your computer and use it in GitHub Desktop.
Save KrisRJack/ff9c30d43aaacaa8ff72a5f32202c40c to your computer and use it in GitHub Desktop.
Haptic feedback in Swift
//
// 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