This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 触觉 iconExpo 触觉

一个提供访问 Android 系统振动效果、iOS 触觉引擎以及 Web 上 Web 振动 API 的库。

Android
iOS
Web
Recommended version:
~58.0.5

expo-haptics 提供以下触觉反馈:

  • 使用 Vibrator 系统服务的 Android 设备。
  • 使用 Taptic Engine 的 iOS 10+ 设备。
  • Web 平台使用 Web Vibration API。

在 iOS 上,如果用户设备上满足以下任一条件,Taptic 引擎将不执行任何操作:

🌐 On iOS, the Taptic engine will do nothing if any of the following conditions are true on a user's device:

  • 低电量模式已启用。可以通过 expo-battery 检测到。
  • 用户在设置中禁用了 Taptic Engine。
  • iOS 相机处于活动状态(以防止不稳定)。
  • iOS 听写处于活动状态(不会干扰麦克风输入)。

在网页上,该库使用 Web 震动 API。请注意以下事项:

🌐 On web, the library uses the Web Vibration API. Note the following:

  • 该 API 必须得到浏览器支持(请检查浏览器兼容性)
  • 设备必须配备振动硬件
  • 用户必须授予使用振动(通常是自动)的权限。
  • 某些浏览器可能会在某些情况下忽略振动(例如,后台标签页)。

安装

🌐 Installation

Terminal
- npx expo install expo-haptics
- yarn expo install expo-haptics
- pnpm expo install expo-haptics
- bun expo install expo-haptics

If you are installing this in an existing React Native app, make sure to install expo in your project.

配置

🌐 Configuration

在 Android 上,该库需要权限来控制设备的振动。VIBRATE 权限会自动添加。

🌐 On Android, this library requires permission to control vibration on the device. The VIBRATE permission is added automatically.

用法

🌐 Usage

Haptics usage
import { StyleSheet, View, Text, Button } from 'react-native'; import * as Haptics from 'expo-haptics'; export default function App() { return ( <View style={styles.container}> <Text style={styles.text}>Haptics.selectionAsync</Text> <View style={styles.buttonContainer}> <Button title="选择" onPress={() => Haptics.selectionAsync()} /> </View> <Text style={styles.text}>Haptics.notificationAsync</Text> <View style={styles.buttonContainer}> <Button title="成功" onPress={ () => Haptics.notificationAsync( Haptics.NotificationFeedbackType.Success ) } /> <Button title="错误" onPress={ () => Haptics.notificationAsync( Haptics.NotificationFeedbackType.Error ) } /> <Button title="警告" onPress={ () => Haptics.notificationAsync( Haptics.NotificationFeedbackType.Warning ) } /> </View> <Text style={styles.text}>Haptics.impactAsync</Text> <View style={styles.buttonContainer}> <Button title="光" onPress={ () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light) } /> <Button title="中等" onPress={ () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium) } /> <Button title="重" onPress={ () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy) } /> <Button title="僵硬" onPress={ () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Rigid) } /> <Button title="柔软" onPress={ () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Soft) } /> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', paddingHorizontal: 16, }, text: { textAlign: 'center', }, buttonContainer: { flexDirection: 'row', alignItems: 'stretch', marginTop: 10, marginBottom: 30, justifyContent: 'space-between', }, });

应用接口

🌐 API

import * as Haptics from 'expo-haptics';

No API data file found, sorry!