react native 中使用 Expo Haptics 实现长按按钮获得震动反馈效果

发布时间 2023-12-19 10:27:54作者: Li_pk

一、背景:

项目中有长按按钮开始录音功能,需要在长按开始时添加一次震动反馈以提高用户体验。由于项目使用expo开发,所以找到了 Expo Haptics 库。
image

二、使用过程:

1. 安装:

npx expo install expo-haptics

2. 引入并使用:

import * as Haptics from "expo-haptics";

const startRecord = () => {
  Haptics.selectionAsync();
  // 其它操作
  ...
}
...
<Pressable onPress={() => startRecord()}>
  <Image
    source={require("../assets/chat/startRecord.png")}
    style={styles.startIcon}
  ></Image>
</Pressable>
...

其它配置和功能可以查看官方文档。

三、文档:

npm 地址
GitHub地址
expo 文档地址