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()
}
}

Founder of Echobatix, developing assistive technology for the blind. echobatix@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The New Look of iOS 15

Looping with a delay in Swift

Implementing the Adapter Design Pattern in Swift

Xcode schemes to manage Staging Production environment in Swift and Objective-C

Swift: Conditional Statement

Table views on SwiftUI — Common tasks

Unit Testing Asynchronous Combine Code in Swift

Recreating Instagram’s Like ❤️Animation in Swift

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
Gary Bartos

Gary Bartos

Founder of Echobatix, developing assistive technology for the blind. echobatix@gmail.com

More from Medium

Add Touch ID & Face ID in Swift

How To Add Cover Image to Swift Playgrounds Project

Swift : Understanding Mutating Functions in One Minute

iOS Fundamentals — iOS Tour Part 2