@capacitor/haptics
Haptics API 通过触感或震动为用户提供物理反馈。
在不支持 Taptic Engine 或振动马达的设备上,API 调用会正常返回但不会执行任何操作。
安装
npm install @capacitor/haptics@latest-5
npx cap sync
示例
import { Haptics, ImpactStyle } from '@capacitor/haptics';
const hapticsImpactMedium = async () => {
await Haptics.impact({ style: ImpactStyle.Medium });
};
const hapticsImpactLight = async () => {
await Haptics.impact({ style: ImpactStyle.Light });
};
const hapticsVibrate = async () => {
await Haptics.vibrate();
};
const hapticsSelectionStart = async () => {
await Haptics.selectionStart();
};
const hapticsSelectionChanged = async () => {
await Haptics.selectionChanged();
};
const hapticsSelectionEnd = async () => {
await Haptics.selectionEnd();
};
API
impact(...)
impact(options?: ImpactOptions | undefined) => Promise<void>
触发触感"碰撞"反馈
| 参数 | 类型 |
|---|---|
options | |
自版本: 1.0.0
notification(...)
notification(options?: NotificationOptions | undefined) => Promise<void>
触发触感"通知"反馈
| 参数 | 类型 |
|---|---|
options | |
自版本: 1.0.0
vibrate(...)
vibrate(options?: VibrateOptions | undefined) => Promise<void>
震动设备
| 参数 | 类型 |
|---|---|
options | |
自版本: 1.0.0
selectionStart()
selectionStart() => Promise<void>
触发选择开始触感提示
自版本: 1.0.0
selectionChanged()
selectionChanged() => Promise<void>
触发选择变更触感提示。如果选择已经启动,此操作将使设备提供触感反馈
自版本: 1.0.0
selectionEnd()
selectionEnd() => Promise<void>
如果已调用 selectionStart(),selectionEnd() 将结束选择。例如,当用户从控件上抬起手指时调用此方法
自版本: 1.0.0
接口
ImpactOptions
| 属性 | 类型 | 描述 | 默认值 | 自版本 |
|---|---|---|---|---|
style | | 碰撞反馈样式 模拟碰撞中物体质量的 UIImpactFeedbackGenerator 对象 | ImpactStyle.Heavy | 1.0.0 |
NotificationOptions
| 属性 | 类型 | 描述 | 默认值 | 自版本 |
|---|---|---|---|---|
type | | 通知反馈类型 由 UINotificationFeedbackGenerator 对象生成的通知反馈类型 | NotificationType.SUCCESS | 1.0.0 |
VibrateOptions
| 属性 | 类型 | 描述 | 默认值 | 自版本 |
|---|---|---|---|---|
duration | number | 震动持续时间(单位:毫秒) | 300 | 1.0.0 |
枚举
ImpactStyle
| 成员 | 值 | 描述 | 自版本 |
|---|---|---|---|
Heavy | 'HEAVY' | 大型、重型用户界面元素之间的碰撞 | 1.0.0 |
Medium | 'MEDIUM' | 中等大小用户界面元素之间的碰撞 | 1.0.0 |
Light | 'LIGHT' | 小型、轻型用户界面元素之间的碰撞 | 1.0.0 |
NotificationType
| 成员 | 值 | 描述 | 自版本 |
|---|---|---|---|
Success | 'SUCCESS' | 表示任务已成功完成的通知反馈类型 | 1.0.0 |
Warning | 'WARNING' | 表示任务产生警告的通知反馈类型 | 1.0.0 |
Error | 'ERROR' | 表示任务已失败的通知反馈类型 | 1.0.0 |