@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 |
|
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 |