跳到主要内容

useVibrate

提供震动反馈

何时使用

当需要操作或者使用震动时

API

const { vibrate, clear } = useVibrate(interval?: boolean, gap?: number);

参数说明

参数说明类型默认值
interval是否持续震动boolean-
gap持续震动间隔number200

返回值说明

返回值说明类型
vibrate操作函数, 可根据参数执行长短震动(long?: boolean) => Promise<General.CallbackResult>
clear若为持续震动可取消震动() => void

代码演示

device/useVibrate/index
import React from 'react';
import { useVibrate } from 'taro-hooks';
import DemoContent from '@src/components/DemoContent';
import { Button } from '@taroify/core';

export default () => {
const { vibrate } = useVibrate();
const { vibrate: vibrateInterval, clear } = useVibrate(true);

return (
<DemoContent>
<Button
block
color="primary"
className="gap"
onClick={() => vibrate()}
shape="square"
>
短震动触感按钮
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => vibrate(true)}
shape="square"
>
长震动触感按钮
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => vibrateInterval()}
shape="square"
>
持续短震动触感按钮
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => vibrateInterval(true)}
shape="square"
>
持续长震动触感按钮
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => clear()}
shape="square"
>
关闭持续震动
</Button>
</DemoContent>
);
};

Hook 支持度

微信小程序H5ReactNative
✔️✔️✔️