React Native 拖动元素的边缘以更改高度

kb5ga3dv  于 2023-01-18  发布在  React
关注(0)|答案(1)|浏览(152)

在我的应用程序中,我有一个框,我想使用GestureHandler更改其高度。我有点明白了,但现在我不知道如何从下到上更改高度。我将附上图片。
我目前的代码-

import { StyleSheet } from "react-native";
import { PanGestureHandler } from "react-native-gesture-handler";
import Animated, {
  useAnimatedGestureHandler,
  useAnimatedStyle,
  useSharedValue,
  withSpring,
} from "react-native-reanimated";
export default function App() {
  const translateY = useSharedValue(44);

  const gestureHandler = useAnimatedGestureHandler({
    onStart: (event, context) => {
      context.translateY = translateY.value;
    },
    onActive: (event, context) => {
      translateY.value = event.translationY + context.translateY;
      if (translateY.value > 200) {
        translateY.value = 200;
      }
    },
    onEnd: (_) => {
      if (translateY.value == 200) {
        translateY.value = withSpring(50);
      }
    },
  });

  const animatedStyle = useAnimatedStyle(() => {
    return {
      height: translateY.value,
    };
  });

  return (
    <PanGestureHandler onGestureEvent={gestureHandler}>
      <Animated.View style={[styles.box, animatedStyle]} />
    </PanGestureHandler>
  );
}

const styles = StyleSheet.create({
  box: {
    width: 65,
    backgroundColor: "black",
    alignSelf: "center",
    marginTop: 350,
  },
});

最终结果如下所示-x1c 0d1x
我想要的运动正好相反

我该怎么做呢?

of1yzvn4

of1yzvn41#

已经很接近了-除了增加高度外,您还需要平移方框的Y位置。您可以将动画样式更改为:

const animatedStyle = useAnimatedStyle(() => {
    return {
      height: translateY.value,
      translate: [{ translateY: -translateY.value }]
    };
  });

相关问题