gpt4 book ai didi

animated.view resets to 0 while using PanGestureHandler from react-native-gesture-handler(在从反应本机手势处理程序使用PanGestureHandler时,Animated.view重置为0)

转载 作者:bug小助手 更新时间:2023-10-28 21:13:44 24 4
gpt4 key购买 nike



I'm building a slideable chat screen similar to the Discord mobile application. In my current implementation, I have an Animated.View that represents the chat screen. When I swipe it to approximately 20% of the screen, it's supposed to automatically slide to 80% of the screen. However, I'm facing an issue where even a slight swipe causes the animation to reset to 0, as shown in this GIF .I'm seeking guidance from the community on how to fix this issue. The problem is that even a small swipe resets the animation to 0, which should not happen. Any suggestions or corrections are appreciated.

我正在构建一个可滑动的聊天屏幕,类似于不一致移动应用程序。在我当前的实现中,我有一个表示聊天屏幕的Animated.View。当我将它滑动到大约20%的屏幕时,它应该会自动滑动到80%的屏幕。然而,我面临着一个问题,即使是轻微的滑动也会导致动画重置为0,如这个GIF所示。我正在寻求社区的指导来解决这个问题。问题是,即使是一次小滑动也会将动画重置为0,这应该不会发生。如有任何建议或更正,欢迎光临。


enter image description here


  const translateX = new Animated.Value(0);
const [direction, setDirection] = useState(null);
const onPanGestureEvent = Animated.event(
[
{
nativeEvent: {
translationX: translateX,
},
},
],
{useNativeDriver: false},
);

const onPanHandlerStateChange = event => {
console.log(event.nativeEvent.translationX);
if (event.nativeEvent.oldState === State.ACTIVE) {
const finalTranslateX = event.nativeEvent.translationX;
const screenWidth = wp('100%');
const translateThreshold = screenWidth * 0.2;

if (finalTranslateX >= translateThreshold) {
// Swipe right
Animated.timing(translateX, {
toValue: screenWidth * 0.8,
duration: 250,
useNativeDriver: false,
}).start();
} else if (finalTranslateX <= -translateThreshold) {
// Swipe left
Animated.timing(translateX, {
toValue: -screenWidth * 0.8,
duration: 250,
useNativeDriver: false,
}).start();
} else if (
finalTranslateX <= translateThreshold ||
finalTranslateX >= translateThreshold
) {
Animated.timing(translateX, {
toValue: -screenWidth * 0,
duration: 250,
useNativeDriver: false,
}).start();
} else {
// No significant swipe, reset to the original position
// Animated.spring(translateX, {
// toValue: 0,
// useNativeDriver: false,
// }).start();
}
}
};

return (
<SafeAreaView style={styles.parent}>
<PanGestureHandler
onGestureEvent={onPanGestureEvent}
onHandlerStateChange={onPanHandlerStateChange}>
<Animated.View
style={[
styles.testBox,
{
transform: [
{
translateX: translateX,
},
],
},
]}
/>
</PanGestureHandler>
</SafeAreaView>
);
};

更多回答
优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com