Skip to content

Instantly share code, notes, and snippets.

@mrugeshtank
Last active April 2, 2021 06:43
Show Gist options
  • Save mrugeshtank/9d7fc3e2dbe5b4554d0169bb40128e5c to your computer and use it in GitHub Desktop.
Save mrugeshtank/9d7fc3e2dbe5b4554d0169bb40128e5c to your computer and use it in GitHub Desktop.
Haptic feedback generator class
import UIKit
class HapticHelper {
enum HapticType {
case impactLight
case impactMedium
case impactHeavy
case selectionChange
case notificationSuccess
case notificationError
case notificaitonWarning
}
static let shared = HapticHelper()
private let impactMediumGenerator = UIImpactFeedbackGenerator(style: UIImpactFeedbackStyle.medium)
private let impactHeavyGenerator = UIImpactFeedbackGenerator(style: UIImpactFeedbackStyle.heavy)
private let impactLightGenerator = UIImpactFeedbackGenerator(style: UIImpactFeedbackStyle.light)
private let selectionGenerator = UISelectionFeedbackGenerator()
private let notificationGenerator = UINotificationFeedbackGenerator()
func generate(feedbackType type: HapticType) {
switch type {
case .impactLight:
impactLightGenerator.prepare()
impactLightGenerator.impactOccurred()
case .impactMedium:
impactMediumGenerator.prepare()
impactMediumGenerator.impactOccurred()
case .impactHeavy:
impactHeavyGenerator.prepare()
impactHeavyGenerator.impactOccurred()
case .selectionChange:
selectionGenerator.prepare()
selectionGenerator.selectionChanged()
case .notificationSuccess:
notificationGenerator.prepare()
notificationGenerator.notificationOccurred(.success)
case .notificationError:
notificationGenerator.prepare()
notificationGenerator.notificationOccurred(.error)
case .notificaitonWarning:
notificationGenerator.prepare()
notificationGenerator.notificationOccurred(.warning)
}
}
}
//usage
/*
in your VC somewhere
HapticHelper.shared.generate(feedbackType: .notificationSuccess)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment