在React Native中,我如何只允许正确方向的panResponder?

时间:2018-07-30 15:03:31

标签: react-native

class WrappedCell extends React.Component{
    translateX = new Animated.Value(0);
    _panResponder = PanResponder.create({
        onMoveShouldSetResponderCapture: () => true,
        onMoveShouldSetPanResponderCapture: () => true,
        onPanResponderMove: (e, gestureState) => { 
            Animated.event([null, {dx: this.translateX}])(e, gestureState)
        },
        onPanResponderTerminate: (e, gestureState) => {
            this._onReleased(e, gestureState);
        },
        onPanResponderRelease: (e, gestureState) => {
            this._onReleased(e, gestureState);
        },
    });

现在,我的平移响应器允许两个方向。如何修改onPanResponderMove,使其仅允许向右方向?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

 onPanResponderMove: (e, gestureState) => {
     if(gestureState.dx > 0)
         Animated.event([null, {dx: this.translateX}])(e, gestureState)
 },