You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.2 KiB
114 lines
2.2 KiB
|
1 month ago
|
/**
|
||
|
|
* 触觉反馈 Hook
|
||
|
|
* 封装 Expo Haptics 功能
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { useCallback } from 'react';
|
||
|
|
import * as Haptics from 'expo-haptics';
|
||
|
1 month ago
|
import { useHapticsEnabled } from '@/stores/settingsStore';
|
||
|
1 month ago
|
|
||
|
|
/**
|
||
|
|
* 触觉反馈 Hook
|
||
|
|
* 根据用户设置决定是否触发触觉反馈
|
||
|
|
*/
|
||
|
|
export function useHaptics() {
|
||
|
|
const hapticsEnabled = useHapticsEnabled();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 轻触反馈
|
||
|
|
*/
|
||
|
|
const light = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 中等反馈
|
||
|
|
*/
|
||
|
|
const medium = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 重触反馈
|
||
|
|
*/
|
||
|
|
const heavy = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 成功反馈
|
||
|
|
*/
|
||
|
|
const success = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 警告反馈
|
||
|
|
*/
|
||
|
|
const warning = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 错误反馈
|
||
|
|
*/
|
||
|
|
const error = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 选择反馈
|
||
|
|
*/
|
||
|
|
const selection = useCallback(async () => {
|
||
|
|
if (hapticsEnabled) {
|
||
|
|
await Haptics.selectionAsync();
|
||
|
|
}
|
||
|
|
}, [hapticsEnabled]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
light,
|
||
|
|
medium,
|
||
|
|
heavy,
|
||
|
|
success,
|
||
|
|
warning,
|
||
|
|
error,
|
||
|
|
selection,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 使用示例:
|
||
|
1 month ago
|
*
|
||
|
1 month ago
|
* function MyComponent() {
|
||
|
|
* const haptics = useHaptics();
|
||
|
1 month ago
|
*
|
||
|
1 month ago
|
* const handlePress = () => {
|
||
|
|
* haptics.light();
|
||
|
|
* // 执行其他操作
|
||
|
|
* };
|
||
|
1 month ago
|
*
|
||
|
1 month ago
|
* const handleSuccess = () => {
|
||
|
|
* haptics.success();
|
||
|
|
* // 显示成功消息
|
||
|
|
* };
|
||
|
1 month ago
|
*
|
||
|
1 month ago
|
* return (
|
||
|
|
* <TouchableOpacity onPress={handlePress}>
|
||
|
|
* <Text>Press me</Text>
|
||
|
|
* </TouchableOpacity>
|
||
|
|
* );
|
||
|
|
* }
|
||
|
|
*/
|