Last active
April 16, 2021 08:34
-
-
Save YanSte/0b3a60c85919449e82ed119061eb7ede to your computer and use it in GitHub Desktop.
Vibration service on iOS
This file contains hidden or 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 Yannick Stephan on 2020-11-21. | |
// | |
import UIKit | |
import AudioToolbox | |
enum VibrationService { | |
case error | |
case success | |
case warning | |
case light | |
case medium | |
case heavy | |
@available(iOS 13.0, *) | |
case soft | |
@available(iOS 13.0, *) | |
case rigid | |
case selection | |
case oldSchool | |
public func vibrate() { | |
switch self { | |
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: | |
if #available(iOS 13.0, *) { | |
UIImpactFeedbackGenerator(style: .soft).impactOccurred() | |
} | |
case .rigid: | |
if #available(iOS 13.0, *) { | |
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