Member-only story
Haptics in UIKit: A Simple Guide
Appear that section!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 15.0, and Swift 5.9
The background to this is that some time ago I needed to implement Haptics. Looking at the files, this was in 2021. It’s probably time I shared my simple guide how to use UINotificationFeedbackGenerator
and UISelectionFeedbackGenerator
.
Using Haptics
Sure, you can use the documentation but I like to have a simple project to see how this could be used. Basically I just wanted a view that would cycle through a load of haptics feedback.
The Code
This is the code for haptics. Nothing too special here, although the implementation is in code only. I’ve used a switch statement so there is only a single haptic at any one time. On with the code!
import UIKit
final class ViewController: UIViewController {
var i = 0
override func loadView() {
let view = UIView()
view.backgroundColor = .red
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)…