This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 触觉反馈
一个库,可在 Android 上访问系统振动效果、在 iOS 上访问触觉引擎,并在 Web 上访问 Web Vibration API。
expo-haptics 为以下平台提供触觉(触摸)反馈:
- 使用 Vibrator 系统服务的 Android 设备。
- 使用 Taptic Engine 的 iOS 10+ 设备。
- 使用 Web Vibration API 的 Web 平台。
在 iOS 上,如果用户设备满足以下任一条件,Taptic engine 将不会执行任何操作:
- 已启用低电量模式。可通过
expo-battery检测。 - 用户在设置中停用了 Taptic Engine。
- iOS 相机处于活动状态(以防止设备不稳定)。
- iOS 听写处于活动状态(以免干扰麦克风输入)。
在 Web 上,此库使用 Web Vibration API。请注意以下事项:
- 浏览器必须支持此 API(请查看浏览器兼容性)
- 设备必须配备振动硬件
- 用户必须授予使用振动功能的权限(通常会自动授予)
- 某些浏览器可能会在特定情境下忽略振动(例如,后台标签页)
如需更高级的触觉反馈控制,我们推荐使用 Pulsar haptics SDK。它可让你创建自定义触觉模式、使用更多预设、构建基于手势的触觉反馈,并在 worklets 上运行触觉反馈。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
配置
在 Android 上,此库需要获得控制设备振动的权限。系统会自动添加 VIBRATE 权限。
用法
API
import * as Haptics from 'expo-haptics';
Methods
Promise<void>A Promise which fulfills once native side haptics functionality is triggered.
See: Android's
VibratorAPI is not recommended for implementing haptics feedback. Instead, you should useperformAndroidHapticsAsync, which is similar to iOS haptic feedback and does not requireVIBRATEpermission.
The kind of notification response used in the feedback.
Promise<void>A Promise which fulfills once native side haptics functionality is triggered.
Used to let a user know when a selection change has been registered.
Promise<void>A Promise which fulfills once native side haptics functionality is triggered.
Enums
AndroidHaptics.Clock_Tick = "clock-tick"The user has pressed either an hour or minute tick of a Clock.
AndroidHaptics.Confirm = "confirm"A haptic effect to signal the confirmation or successful completion of a user interaction.
AndroidHaptics.Context_Click = "context-click"The user has performed a context click on an object.
AndroidHaptics.Drag_Start = "drag-start"The user has started a drag-and-drop gesture. The drag target has just been "picked up".
AndroidHaptics.Gesture_End = "gesture-end"The user has finished a gesture (for example, on the soft keyboard).
AndroidHaptics.Gesture_Start = "gesture-start"The user has started a gesture (for example, on the soft keyboard).
AndroidHaptics.Keyboard_Press = "keyboard-press"The user has pressed a virtual or software keyboard key.
AndroidHaptics.Keyboard_Release = "keyboard-release"The user has released a virtual keyboard key.
AndroidHaptics.Long_Press = "long-press"The user has performed a long press on an object that results in an action being performed.
AndroidHaptics.Reject = "reject"A haptic effect to signal the rejection or failure of a user interaction.
AndroidHaptics.Segment_Frequent_Tick = "segment-frequent-tick"The user is switching between a series of many potential choices. For example, minutes on a clock face or individual percentages. This constant is expected to be very soft, so as not to be uncomfortable when performed a lot in quick succession. If the device can't make a suitably soft vibration, then it may not make any vibration.
AndroidHaptics.Segment_Tick = "segment-tick"The user is switching between a series of potential choices. For example, items in a list or discrete points on a slider.
AndroidHaptics.Text_Handle_Move = "text-handle-move"The user has performed a selection/insertion handle move on text field.
AndroidHaptics.Toggle_Off = "toggle-off"The user has toggled a switch or button into the off position.
AndroidHaptics.Toggle_On = "toggle-on"The user has toggled a switch or button into the on position.
AndroidHaptics.Virtual_Key = "virtual-key"The user has pressed on a virtual on-screen key.
The mass of the objects in the collision simulated by a UIImpactFeedbackGenerator object
UINotificationFeedbackStyle.
ImpactFeedbackStyle.Medium = "medium"A collision between moderately sized user interface elements.
ImpactFeedbackStyle.Rigid = "rigid"A collision between user interface elements that are rigid, exhibiting a small amount of compression or elasticity.
The type of notification feedback generated by a UINotificationFeedbackGenerator object.
UINotificationFeedbackType.
NotificationFeedbackType.Error = "error"A notification feedback type indicating that a task has failed.
NotificationFeedbackType.Success = "success"A notification feedback type indicating that a task has completed successfully.