fix:修复心情日历无法打开的问题

This commit is contained in:
richarjiang
2025-09-26 08:54:02 +08:00
parent ad98d78e18
commit badd68c039

View File

@@ -7,14 +7,13 @@ import {
View, View,
} from 'react-native'; } from 'react-native';
import { import {
PanGestureHandler, Gesture,
PanGestureHandlerGestureEvent, GestureDetector,
} from 'react-native-gesture-handler'; } from 'react-native-gesture-handler';
import Animated, { import Animated, {
interpolate, interpolate,
interpolateColor, interpolateColor,
runOnJS, runOnJS,
useAnimatedGestureHandler,
useAnimatedStyle, useAnimatedStyle,
useSharedValue, useSharedValue,
withSpring, withSpring,
@@ -54,18 +53,18 @@ export default function MoodIntensitySlider({
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light); Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
}; };
const gestureHandler = useAnimatedGestureHandler< const startX = useSharedValue(0);
PanGestureHandlerGestureEvent, const lastValue = useSharedValue(value);
{ startX: number; lastValue: number }
>({ const gestureHandler = Gesture.Pan()
onStart: (_, context) => { .onBegin(() => {
context.startX = translateX.value; startX.value = translateX.value;
context.lastValue = value; lastValue.value = value;
isDragging.value = withSpring(1); isDragging.value = withSpring(1);
runOnJS(triggerHaptics)(); runOnJS(triggerHaptics)();
}, })
onActive: (event, context) => { .onUpdate((event) => {
const newX = context.startX + event.translationX; const newX = startX.value + event.translationX;
const clampedX = Math.max(0, Math.min(sliderWidth, newX)); const clampedX = Math.max(0, Math.min(sliderWidth, newX));
translateX.value = clampedX; translateX.value = clampedX;
@@ -73,13 +72,13 @@ export default function MoodIntensitySlider({
const currentValue = Math.round((clampedX / sliderWidth) * (max - min) + min); const currentValue = Math.round((clampedX / sliderWidth) * (max - min) + min);
// 当值改变时触发震动和回调 // 当值改变时触发震动和回调
if (currentValue !== context.lastValue) { if (currentValue !== lastValue.value) {
context.lastValue = currentValue; lastValue.value = currentValue;
runOnJS(triggerHaptics)(); runOnJS(triggerHaptics)();
runOnJS(onValueChange)(currentValue); runOnJS(onValueChange)(currentValue);
} }
}, })
onEnd: () => { .onEnd(() => {
// 计算最终值并吸附到最近的步长 // 计算最终值并吸附到最近的步长
const currentValue = Math.round((translateX.value / sliderWidth) * (max - min) + min); const currentValue = Math.round((translateX.value / sliderWidth) * (max - min) + min);
const snapPosition = ((currentValue - min) / (max - min)) * sliderWidth; const snapPosition = ((currentValue - min) / (max - min)) * sliderWidth;
@@ -88,7 +87,6 @@ export default function MoodIntensitySlider({
isDragging.value = withSpring(0); isDragging.value = withSpring(0);
runOnJS(triggerHaptics)(); runOnJS(triggerHaptics)();
runOnJS(onValueChange)(currentValue); runOnJS(onValueChange)(currentValue);
},
}); });
const thumbStyle = useAnimatedStyle(() => { const thumbStyle = useAnimatedStyle(() => {
@@ -136,29 +134,6 @@ export default function MoodIntensitySlider({
}; };
}); });
// 动态颜色配置 - 根据进度变化颜色
const getProgressColors = (progress: number) => {
if (progress <= 0.25) {
return ['#22c55e', '#84cc16'] as const; // 绿色到浅绿色
} else if (progress <= 0.5) {
return ['#84cc16', '#eab308'] as const; // 浅绿色到黄色
} else if (progress <= 0.75) {
return ['#eab308', '#f97316'] as const; // 黄色到橙色
} else {
return ['#f97316', '#ef4444'] as const; // 橙色到红色
}
};
const progressColorsStyle = useAnimatedStyle(() => {
const progress = translateX.value / sliderWidth;
return {
backgroundColor: interpolateColor(
progress,
[0, 0.25, 0.5, 0.75, 1],
['#22c55e', '#84cc16', '#eab308', '#f97316', '#ef4444']
),
};
});
return ( return (
<View style={styles.container}> <View style={styles.container}>
@@ -173,18 +148,19 @@ export default function MoodIntensitySlider({
/> />
</View> </View>
{/* 进度条 - 动态颜色 */} {/* 进度条 - 动态渐变颜色 */}
<Animated.View style={[styles.progress, { height }, progressStyle, progressColorsStyle]}> <Animated.View style={[styles.progress, { height }, progressStyle]}>
<LinearGradient <LinearGradient
colors={getProgressColors(translateX.value / sliderWidth)} colors={['#22c55e', '#84cc16', '#eab308', '#f97316', '#ef4444']}
locations={[0, 0.25, 0.5, 0.75, 1]}
style={[styles.progressGradient, { height }]} style={[styles.progressGradient, { height }]}
start={{ x: 1, y: 0 }} start={{ x: 0, y: 0 }}
end={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
/> />
</Animated.View> </Animated.View>
{/* 可拖拽的thumb */} {/* 可拖拽的thumb */}
<PanGestureHandler onGestureEvent={gestureHandler}> <GestureDetector gesture={gestureHandler}>
<Animated.View style={[styles.thumb, { width: thumbSize, height: thumbSize }, thumbStyle]}> <Animated.View style={[styles.thumb, { width: thumbSize, height: thumbSize }, thumbStyle]}>
{/* <LinearGradient {/* <LinearGradient
colors={['#ffffff', '#f8fafc']} colors={['#ffffff', '#f8fafc']}
@@ -194,7 +170,7 @@ export default function MoodIntensitySlider({
/> */} /> */}
<Animated.View style={[styles.thumbInner, thumbInnerStyle]} /> <Animated.View style={[styles.thumbInner, thumbInnerStyle]} />
</Animated.View> </Animated.View>
</PanGestureHandler> </GestureDetector>
</View> </View>
{/* 标签 */} {/* 标签 */}