The abstract superclass for all feedback generators.
- iOS 10.0+
Do not subclass or create instances of this class yourself. Instead, instantiate one of the provided concrete subclasses:
UIImpact. 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.
UISelection. Use selection feedback generators to indicate a change in selection.
UINotification. Use notification feedback generators to indicate successes, failures, and warnings.
Using Feedback Generators
Haptic feedback provides a tactile response, such as a tap, that draws attention and reinforces both actions and events. 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.
When providing 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.
To use a feedback generator, the following are required:
Preparing the Generator (optional)
Releasing the Generator (optional).
Listing 1 demonstrates triggering selection feedback from a pan gesture recognizer.
Instantiating the Generator
To create feedback, you must first instantiate one of the
UIFeedback class’s concrete subclasses (
Preparing the Generator
Preparing the generator can reduce latency when triggering feedback. 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. To preserve power, the Taptic Engine stays in this state for only a short period of time (on the order of seconds), or until you next trigger feedback.
Think about when and where you can best prepare your generators. If you call
prepare() and then immediately trigger feedback, the system won’t have enough time to get the Taptic Engine into the prepared state, and you may not see a reduction in latency. On the other hand, if you call
prepare() too early, the Taptic Engine may become idle again before you trigger feedback.
Listing 1 prepares the generator when the pan gesture begins, and then triggers feedback when the gesture changes.
For more information, see the
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.
In Listing 1, each time the gesture changes the gesture handler checks to see if the selection has changed. If so, the handler triggers feedback and prepares for the next change.
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.
In Listing 1, the pan gesture recognizer releases its generator when the gesture ends, fails, or is canceled. Assigning
nil to the instance variable removes the reference to the old generator. If there are no other references to the generator, the system deallocates it.