How to add Haptic feedback using Taptic Engine in iOS Swift?

The Taptic Engine is Apple’s implementation of haptic user interface feedback. This is available from iPhone 7 and above. Let’s understand more about it. Taptic Engine is available from iphone 6s, but iphone 7 has a better version of it.

Abhimuralidharan
Mar 12 · 4 min read

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?

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

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?

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: .light, .medium, and .heavy.
  • UISelectionFeedbackGenerator. Use selection feedback generators to indicate a change in selection. It has three variations:.success, .warning, and .error.
  • UINotificationFeedbackGenerator. Use notification feedback generators to indicate successes, failures, and warnings.

Using Feedback Generators

  1. Instantiating the Generator: To create feedback, you must first instantiate one of the UIFeedbackGenerator class’s concrete subclasses
  2. 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.
  3. Triggering Feedback: Each feedback generator subclass has a unique triggering method. To trigger feedback, call the appropriate method: impactOccurred(), selectionChanged(), or 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.
  4. Releasing the Generator (optional): Remove all the references so that the system can deallocate the generator instance.

Feedback generators in action!

UIImpactFeedbackGenerator

let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: .light) 
impactFeedbackgenerator.prepare()
impactFeedbackgenerator.impactOccurred()

UISelectionFeedbackGenerator

let selectionFeedbackGenerator = UISelectionFeedbackGenerator()
selectionFeedbackGenerator.selectionChanged()

UINotificationFeedbackGenerator

let notificationFeedbackGenerator = UINotificationFeedbackGenerator()notificationFeedbackGenerator.prepare()notificationFeedbackGenerator.notificationOccurred(.success)
notificationFeedbackGenerator.notificationOccurred(.warning)
notificationFeedbackGenerator.notificationOccurred(.error)

Keep in mind:

  • 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!

Abhimuralidharan

Written by

iOS and tvOS developer, dreamer, photographer 🤨