跳到主要内容
版本:v3

@capacitor/haptics

Haptics API 通过触摸或振动向用户提供物理反馈。

在沒有 Taptic Engine 或振动器的设备上,API 调用将 resolve 而不执行任何操作。

安装​

npm install @capacitor/haptics
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>

触发触觉"冲击"反馈

参数Type
options
ImpactOptions

始于: 1.0.0


notification(...)​

notification(options?: NotificationOptions | undefined) => Promise<void>

触发触觉"通知"反馈

参数Type
options
NotificationOptions

始于: 1.0.0


vibrate(...)​

vibrate(options?: VibrateOptions | undefined) => Promise<void>

使设备振动

参数Type
options
VibrateOptions

始于: 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​

属性Type描述默认值始于
style
ImpactStyle
冲击反馈样式。由 UIImpactFeedbackGenerator 对象模拟的碰撞中物体的质量。ImpactStyle.Heavy1.0.0

NotificationOptions​

属性Type描述默认值始于
type
NotificationType
通知反馈类型。由 UINotificationFeedbackGenerator 对象生成的振动反馈类型。NotificationType.SUCCESS1.0.0

VibrateOptions​

属性Type描述默认值始于
durationnumber振动持续时间,以毫秒为单位。iOS 12 及更早版本不支持。3001.0.0

枚举​

ImpactStyle​

成员Value描述始于
Heavy'HEAVY'大型、重型用户界面元素之间的碰撞1.0.0
Medium'MEDIUM'中等大小用户界面元素之间的碰撞1.0.0
Light'LIGHT'小型、轻型用户界面元素之间的碰撞1.0.0

NotificationType​

成员Value描述始于
Success'SUCCESS'指示任务已成功完成的通知反馈类型1.0.0
Warning'WARNING'指示任务产生了警告的通知反馈类型1.0.0
Error'ERROR'指示任务失败的通知反馈类型1.0.0