@capacitor/haptics
Haptics API 通过触摸或振动为用户提供物理反馈。
在不支持 Taptic Engine 或振动器的设备上,API 调用会正常返回但不会执行任何操作。
安装
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>
触发触觉反馈的"冲击"效果
参数 | 类型 |
---|---|
options |
|
Since: 1.0.0
notification(...)
notification(options?: NotificationOptions | undefined) => Promise<void>
触发触觉反馈的"通知"效果
参数 | 类型 |
---|---|
options |
|
Since: 1.0.0
vibrate(...)
vibrate(options?: VibrateOptions | undefined) => Promise<void>
振动设备
参数 | 类型 |
---|---|
options |
|
Since: 1.0.0
selectionStart()
selectionStart() => Promise<void>
触发选择开始的触觉提示
Since: 1.0.0
selectionChanged()
selectionChanged() => Promise<void>
触发选择变化的触觉提示。如果选择已经开始, 此操作将使设备提供触觉反馈
Since: 1.0.0
selectionEnd()
selectionEnd() => Promise<void>
如果已调用 selectionStart(),selectionEnd() 将结束选择。 例如,当用户从控件上抬起手指时调用此方法
Since: 1.0.0
Interfaces
ImpactOptions
属性 | 类型 | 描述 | 默认值 | Since |
---|---|---|---|---|
style |
| 冲击反馈样式 通过 UIImpactFeedbackGenerator 对象模拟碰撞中物体的质量。 | ImpactStyle.Heavy | 1.0.0 |
NotificationOptions
属性 | 类型 | 描述 | 默认值 | Since |
---|---|---|---|---|
type |
| 通知反馈类型 由 UINotificationFeedbackGenerator 对象生成的通知反馈类型。 | NotificationType.SUCCESS | 1.0.0 |
VibrateOptions
属性 | 类型 | 描述 | 默认值 | Since |
---|---|---|---|---|
duration | number | 振动持续时间(毫秒)。 | 300 | 1.0.0 |
Enums
ImpactStyle
成员 | 值 | 描述 | Since |
---|---|---|---|
Heavy | 'HEAVY' | 大型、重型用户界面元素之间的碰撞 | 1.0.0 |
Medium | 'MEDIUM' | 中等大小用户界面元素之间的碰撞 | 1.0.0 |
Light | 'LIGHT' | 小型、轻型用户界面元素之间的碰撞 | 1.0.0 |
NotificationType
成员 | 值 | 描述 | Since |
---|---|---|---|
Success | 'SUCCESS' | 表示任务已成功完成的通知反馈类型 | 1.0.0 |
Warning | 'WARNING' | 表示任务产生警告的通知反馈类型 | 1.0.0 |
Error | 'ERROR' | 表示任务已失败的通知反馈类型 | 1.0.0 |