Compose and play haptic patterns to customize your iOS app's haptic feedback.
- iOS 13.0+Beta
- UIKit for Mac 13.0+Beta
Core Haptics lets you add customized haptic and audio feedback to your app. Use haptics to engage users physically, with tactile and audio feedback that gets attention and reinforces actions. Some system-provided interface elements—like pickers, switches, and sliders—automatically provide haptic feedback as users interact with them. With Core Haptics, you extend this functionality by composing and combining haptics beyond the default patterns.
Your app can play custom haptic patterns crafted from basic building blocks called haptic events (
CHHaptic). Events can be transient, like the feedback you get from toggling a switch, or continuous, like the vibration or sound from a ringtone. You can use transient and continuous patterns independently, or build your pattern from precise combinations of the two. For example, the following haptic pattern combines a series of transient haptic events, shown in blue as short impulses, with a single continuous haptic event, shown in orange:
Intensity maps to the haptic's amplitude or strength. Sharpness maps to the persistence or frequency of the haptic pattern.
The Core Haptics engine also supports an event type that allows you to play customized audio content as part of your pattern.
To add haptics to your app, you create an instance of
CHHaptic, load a pattern, and use the engine to create a player to play that pattern. You create a pattern in one of three ways:
Dictionaries. Each entry in the dictionary defines a single characteristic of the haptic, like its intensity, start time, or duration. See Playing a Single-Tap Haptic Pattern to learn more about creating a dictionary inline.
Arrays of events and parameters. The
CHHapticclass represents a haptic event as an object in code. The key-value pairs in a dictionary correspond to the properties and parameters associated with a
CHHaptic. Haptic event objects are just another representation of the haptics dictionary.
AHAP files. This JSON-compliant file format specifies a haptic pattern through key-value pairs, analogous to a dictionary literal, except in a text file. Add this file to your Xcode project bundle.
All forms of pattern creation result in patterns with the exact same kind of content.