How to use Haptic Feedback for a better experience in iOS?

Batikan Sosun
4 min readOct 7, 2019

--

The image comes from great Morten Niklasson

Experience is everything. When you buy and download an app, you deeply review it for useful. Useful or not? Design, font, text size, icon, animation, vibration, etc. play a role in providing a better experience.

On the other hand, Haptics also affects people’s sense of touch to enhance the experience of interacting with on-screen interfaces. For example, when a payment transaction is approved, the system plays haptic as well as providing visual and audio feedback. Haptics can also increase touch gestures and interactions, such as switching between shifting or changing a selector.

In this article, we are going to deeply look to Apple’s implementation Taptic Engine. The Taptic Engine is available from 6S or later, but iPhone 7 or later has a better structure of it.

The iPhone has previously used the following code for vibrations. The following code work on all devices.

Unsplash

Here we go:

Basic vibration on iOS device

import AudioToolbox.AudioServicesAudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

Warning: According to the SDK header file; this function will be deprecated in a future release. Use AudioServicesPlaySystemSoundWithCompletion instead.

For example:

AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate) { }

You can find here a list of sound IDs available on iOS devices.

iPhone 6S or later comes with a Taptic Engine that allows us to more feels feedback to users.

Some standard UI elements — like switches, sliders, and pickers — that play Apple-designed system haptics by default. You can use them to add your own feedback to custom views and controls.

Using Feedback Generators

The abstract superclass for all feedback generators. Available from UIKit and iOS 10 or later.

Reference: Apple Documentation link.

@available(iOS 10.0, *)
open class UIFeedbackGenerator : NSObject

It is important according to Apple: Do not subclass or create instances of this class yourself. Instead, instantiate one of the provided concrete subclasses.

We can use any of the following concrete subclasses to trigger 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.
  • UISelectionFeedbackGenerator. Use selection feedback generators to indicate a change in selection.
  • UINotificationFeedbackGenerator. Use notification feedback generators to indicate successes, failures, and warnings.

Let’s Dive in 🚀

UIImpactFeedbackGenerator

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

The Impact Feedback style has three variations. These are:

public enum FeedbackStyle : Int {case lightcase mediumcase heavy}

UISelectionFeedbackGenerator

let selectionFeedbackGenerator = UISelectionFeedbackGenerator()selectionFeedbackGenerator.selectionChanged()selectionFeedbackGenerator = nil

UINotificationFeedbackGenerator

let notificationFeedbackGenerator = UINotificationFeedbackGenerator()notificationFeedbackGenerator.prepare()
notificationFeedbackGenerator.notificationOccurred(.success)
notificationFeedbackGenerator = nil

The Notification Feedback type has three variations. These are:

public enum FeedbackType : Int {case successcase warningcase error}

If you want to include sound along with the haptic feedback, you need to manually play the sound and sync it with the haptics.

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.

For example, haptic feedback is currently played only:

  • On a device with a supported Taptic Engine
  • When the app is running in the foreground
  • When 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.

Releasing the Generator

If you no longer need a prepared generator, remove all references to the generator object and let the system deallocate it. This lets the Taptic Engine return to its idle state.

If you have any criticism, question, or suggestion, feel free to post them in the comment section below!

Thanks for reading.

Some open-source projects I have created.

--

--

Batikan Sosun
Batikan Sosun

Written by Batikan Sosun

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging

No responses yet