@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 |
|
自版本: 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
Interfaces
ImpactOptions
属性 | 类型 | 描述 | 默认值 | 版本 |
---|---|---|---|---|
style |
| 碰撞反馈风格 模拟UIImpactFeedbackGenerator对象碰撞效果的质量 | ImpactStyle.Heavy | 1.0.0 |
NotificationOptions
属性 | 类型 | 描述 | 默认值 | 版本 |
---|---|---|---|---|
type |
| 通知反馈类型 由UINotificationFeedbackGenerator对象生成的通知反馈类型 | NotificationType.SUCCESS | 1.0.0 |
VibrateOptions
属性 | 类型 | 描述 | 默认值 | 版本 |
---|---|---|---|---|
duration | number | 振动持续时间(毫秒) | 300 | 1.0.0 |
Enums
ImpactStyle
成员 | 值 | 描述 | 版本 |
---|---|---|---|
Heavy | 'HEAVY' | 大型、重型UI元素之间的碰撞效果 | 1.0.0 |
Medium | 'MEDIUM' | 中等大小UI元素之间的碰撞效果 | 1.0.0 |
Light | 'LIGHT' | 小型、轻型UI元素之间的碰撞效果 | 1.0.0 |
NotificationType
成员 | 值 | 描述 | 版本 |
---|---|---|---|
Success | 'SUCCESS' | 表示任务已成功完成的通知反馈类型 | 1.0.0 |
Warning | 'WARNING' | 表示任务产生警告的通知反馈类型 | 1.0.0 |
Error | 'ERROR' | 表示任务失败的通知反馈类型 | 1.0.0 |