Haptics extension to UIViewController in Swift

import UIKit

/// Look into calling generator.prepare()
/// https://developer.apple.com/documentation/uikit/uifeedbackgenerator
extension UIViewController {

@objc func hapticError() {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
}

@objc func hapticSuccess() {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}

@objc func hapticWarning() {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
}

@objc func hapticImpactLight() {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
}

@objc func hapticImpactMedium() {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}

@objc func hapticImpactHeavy() {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
}

@objc func hapticSelectionChanged() {
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store