react-native-vibration-plus
v1.0.2
Published
React Native vibration feedback
Maintainers
Readme
react-native-vibration-plus
触觉反馈组件,提供多种震动类型的统一 API。
安装
npm install react-native-vibration-plusiOS 需要执行 pod install:
cd ios && pod install使用
import { haptic, onVibration } from 'react-native-vibration-plus';
// 触发轻量震动(等同于 onVibration)
haptic('light');
// 其他震动类型
haptic('medium');
haptic('heavy');
haptic('selection');
haptic('success');
haptic('warning');
haptic('error');
// 兼容旧版 API
onVibration();
// 仅在 Android 触发震动,iOS 跳过
onVibration(true);API
haptic(type?)
触发指定类型的触觉反馈。
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| type | 'light' \| 'medium' \| 'heavy' \| 'success' \| 'warning' \| 'error' \| 'selection' | 'light' | 震动类型 |
震动类型说明:
| 类型 | 说明 |
|------|------|
| light | 轻量冲击 |
| medium | 中等冲击 |
| heavy | 重度冲击 |
| selection | 选择反馈 |
| success | 操作成功通知 |
| warning | 警告通知 |
| error | 错误通知 |
onVibration(onlyAndroid?)
兼容旧版 API,内部调用 haptic('light')。
| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | onlyAndroid | boolean | false | 为 true 时仅 Android 触发震动,iOS 不执行 |
实现原理
- Android:使用 React Native 内置的
Vibration.vibrate()配合不同时长/模式的震动模式selection→ 10ms,light→ 20ms,medium→ 40ms,heavy→ 80mssuccess/warning/error→ 使用多段震动模式
- iOS:通过原生模块调用不同反馈生成器(iOS 10+)
light/medium/heavy→UIImpactFeedbackGeneratorsuccess/warning/error→UINotificationFeedbackGeneratorselection→UISelectionFeedbackGenerator- iOS 10 以下降级使用
AudioServicesPlaySystemSound
