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

/**
* Hook
* Expo Haptics
*/
import { useCallback } from 'react';
import * as Haptics from 'expo-haptics';
1 month ago
import { useHapticsEnabled } from '@/stores/settingsStore';
/**
* 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
*
* function MyComponent() {
* const haptics = useHaptics();
1 month ago
*
* const handlePress = () => {
* haptics.light();
* // 执行其他操作
* };
1 month ago
*
* const handleSuccess = () => {
* haptics.success();
* // 显示成功消息
* };
1 month ago
*
* return (
* <TouchableOpacity onPress={handlePress}>
* <Text>Press me</Text>
* </TouchableOpacity>
* );
* }
*/