具有功能组件的PanGestureHandler反应本机

时间:2020-07-30 19:18:38

标签: react-native react-native-ios react-native-gesture-handler

我正在尝试将手势处理程序与功能组件一起使用。问题是当我第二次拖动时,它再次从初始位置拖动

enter image description here

这是我的下面的代码

let translateXRef = useRef(new Animated.Value(0)).current;

const onGestureEvent = useCallback(
    Animated.event(
      [
        {
          nativeEvent: {
            translationX: translateXRef,
          },
        },
      ],
      { useNativeDriver: true }
    ),
    []
  );

<View
    style={{
      backgroundColor: '#FFFFFF80',
      position: 'absolute',
      top: 0,
      bottom: 0,
      left: 0,
      right: 0,
      height: 100,
    }}
  >
    <PanGestureHandler
      onGestureEvent={onGestureEvent}
      onHandlerStateChange={onHandlerStateChange}
    >
      <Animated.View
        // eslint-disable-next-line react-native/no-inline-styles
        style={{
          height: '100%',
          width: 10,
          backgroundColor: AppColors.buttonColor,
          transform: [{ translateX: translateXRef }],
        }}
      />
    </PanGestureHandler>
  </View>

0 个答案:

没有答案