@capacitor/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 | ImpactOptions |
起點 1.0.0
notification(...)
notification(options?: NotificationOptions | undefined) => Promise<void>
觸發觸覺「通知」反饋
參數 | 類型 |
---|---|
options | NotificationOptions |
起點 1.0.0
vibrate(...)
vibrate(options?: VibrateOptions | undefined) => Promise<void>
使裝置震動
參數 | 類型 |
---|---|
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
屬性 | 類型 | 描述 | 預設值 | 起點 |
---|---|---|---|---|
style | ImpactStyle | 衝擊回饋樣式 UIImpactFeedbackGenerator 物件模擬的碰撞中物件的質量。 | ImpactStyle.Heavy | 1.0.0 |
NotificationOptions
屬性 | 類型 | 描述 | 預設值 | 起點 |
---|---|---|---|---|
type | NotificationType | 通知回饋類型 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 |