Creating an awesome and interesting user experience requires more than a nice looking UI. Developers we can make the iOS App Feel better by implementing haptic feedback with the Taptic Engine.
If you have an iPhone 7 or above you may have noticed a small vibration of the device while scrolling a date picker or when changing the value of a switch or do pull to refresh in youtube—that’s haptic feedback. In this tutorial, we can see how to implement haptic feedback with the Taptic Engine. Let’s Dive in
There are a few different types of Haptic Feedback:
important things we should consider before adding haptic feedback
* Always use feedback for its intended purpose. Don’t select a haptic because of the way it feels.
* The source of the feedback must be clear to the user. For example, the feedback must match a visual change in the user interface or must be in response to a user action. Feedback should never come as a surprise.
* Don’t overuse feedback. Overuse can cause confusion and diminish the feedback’s significance.
Use impact feedback generators to indicate that an impact has occurred. For example, you might trigger impact feedback when a user interface object collides with something or snaps into place. UIImpactFeedbackGenerator allows you to provide “light”, “medium” or “heavy” taps based on the user’s action and your selection.
let generator = UIImpactFeedbackGenerator(style: .light) //Based on the impact we can change the impact .medium , .heavy generator.prepare() generator.impactOccurred()
Use selection feedback generators to indicate a change in selection. UISelectionFeedbackGenerator can be seen in the Reminder app when user choosing a date from the date picker.
let generator = UISelectionFeedbackGenerator() generator.selectionChanged()
By using UINotificationFeedbackGenerator we can give feedback to user in the event of an “Error”, “Warning” or “Success”.
//Based on the operation we can specify the notification feedback as .error, .warning and .success let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.error) generator.notificationOccurred(.warning) generator.notificationOccurred(.success)
The sample app is very simple and it covers almost all common use cases, doesn’t consider all the use cases you will need in your app; keep that in mind before loading the copy-paste machine gun.