How to add Haptic feedback using Taptic Engine in iOS Swift?
The iPhone has previously used the linear actuator for vibrations — this has shifted over to the Taptic Engine in the iPhone 6s and above. Apple applies its Taptic Engine to mimic a home button press, relay system notifications, and reinforce audio alerts. But the feedback from the system feels natural, and in sync with what is being presented on screen.
How to do a basic vibration on iOS devices?
AudioToolbox.AudioServices helps us to vibrate the phone using the following code. This works on all devices.
Here is a LIST OF SOUND_IDs available in iOS devices.
Iphone 6s and above comes with taptic engine along with the introduction of 3D touch. iPhone 7 and above has a newer version of taptic engine which is better compared to the one in 6s. iPhone 7 and above doesn’t have a physical home button and hence, Apple had to come up with a more natural haptic feedback using the upgraded taptic engine.
While many system-provided interface elements (for example, pickers, switches, and sliders) automatically provide haptic feedback, you can use feedback generators to add your own feedback to custom views and controls.
How to generate haptic feedback?
Reference: Apple Doc link.
The haptic feedback functionality is provided by
UIFeedbackGenerator class. This is available in UIKit from iOS 10.0+. But
UIFeedbackGenerator is an abstract class and we cannot use this directly or by subclassing.
class UIFeedbackGenerator : NSObject
From apple Docs: Do not subclass or create instances of this class yourself. Instead, instantiate one of the provided concrete subclasses.
We should be using any of the following concrete subclasses to trigger a haptic feedback:
UIImpactFeedbackGenerator. 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. It has three variations:
UISelectionFeedbackGenerator. Use selection feedback generators to indicate a change in selection. It has three variations:
UINotificationFeedbackGenerator. Use notification feedback generators to indicate successes, failures, and warnings.
Using Feedback Generators
To use a feedback generator, the following are required and this applies to all the above-mentioned feedback generators.
- Instantiating the Generator: To create feedback, you must first instantiate one of the
UIFeedbackGeneratorclass’s concrete subclasses
- Preparing the Generator (optional): This step is optional. This will reduce the latency (feedback trigger time delay) to some extent. This is particularly important when trying to match feedback to sound or visual cues. Calling the generator’s
prepare()method puts the Taptic Engine in a prepared state.
- Triggering Feedback: Each feedback generator subclass has a unique triggering method. To trigger feedback, call the appropriate method:
notificationOccurred(_:). Note that calling these methods does not play haptics directly. Instead, it informs the system of the event. The system then determines whether to play the haptics based on the device, the application’s state, the amount of battery power remaining, and other factors.
- Releasing the Generator (optional): Remove all the references so that the system can deallocate the generator instance.
Feedback generators in action!
let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .light)
let selectionFeedbackGenerator = UISelectionFeedbackGenerator()
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()notificationFeedbackGenerator.prepare()notificationFeedbackGenerator.notificationOccurred(.success)
Keep in mind:
- 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.
- Haptic feedback will work only if the app is running in the foreground, On a device with a supported Taptic Engine and the System-Haptics setting is enabled.
- As a general rule, trust the system to determine whether it should play feedback. Don’t check the device type or app state to conditionally trigger feedback. After you’ve decided how you want to use feedback, always trigger it when the appropriate events occur. The system ignores any requests that it cannot fulfill.
Please read about the do’s and dont’s about the haptic feedback from iOS Human Interface guidelines here. This is a must-read.
That’s it. !
If you enjoyed reading this post, please share and give some claps so others can find it 👏👏👏👏👏 !!!!
If you have any comment, question, or recommendation, feel free to post them in the comment section below!