长按后可以在ScrollView中拖动元素

Prz*_*ota 5 native ios reactjs react-native

我已经使用panResponder和ScrollView实现了拖放列表。我希望即使在触摸项目时也可以滚动列表。问题是当我做手势滚动时项目会移动。当然,我也希望能够移动该项目,但是现在它具有与滚动相同的手势。我想通过仅在长按(1.5秒)后才拖动元素来克服它。如何执行呢?我认为可以将Touchable用作onPressIn / onPressOut的元素,如此处所述:http : //browniefed.com/blog/react-native-press-and-hold-button-actions/并以某种方式在该时间段后启用panResponder ,但我不知道如何以编程方式启用它。

现在,这是我在列表中的元素代码:

class AccountItem extends Component {

  constructor(props) {
    super(props);
    this.state = {
      pan: new Animated.ValueXY(),
      zIndex: 0,
    }

    this.panResponder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onPanResponderGrant: (e, gestureState) => {
        this.setState({ zIndex: 100 });
        this.props.disableScroll();
      },
      onPanResponderMove: Animated.event([null, {
        dx: this.state.pan.x,
        dy: this.state.pan.y,
      }]),
      onPanResponderRelease: (e, gesture) => {
        this.props.submitNewPositions();
        Animated.spring(
          this.state.pan,
          {toValue:{ x:0, y:0 }}
        ).start();
        this.setState({ zIndex: 0 });
        this.props.enableScroll();
      }
    })
  }

  meassureMyComponent = (event) => {
    const { setElementPosition } = this.props;
    let posY = event.nativeEvent.layout.y;
    setElementPosition(posY);
  }

  render() {
    const {name, index, onChangeText, onRemoveAccount} = this.props;

    return (
        <Animated.View
          style={[this.state.pan.getLayout(), styles.container, {zIndex: this.state.zIndex}]}
          {...this.panResponder.panHandlers}
          onLayout={this.meassureMyComponent}
        >

some other components...

        </Animated.View>
    )
  }
}

export default AccountItem;
Run Code Online (Sandbox Code Playgroud)

Kev*_* Li 5

我和你遇到了同样的问题。我的解决方案是panResponderonLongPress正常行为定义 2 个不同的处理程序。

  _onLongPressPanResponder(){
    return PanResponder.create({
      onPanResponderTerminationRequest: () => false,
      onStartShouldSetPanResponderCapture: () => true,
      onPanResponderMove: Animated.event([
        null, {dx: this.state.pan.x, dy: this.state.pan.y},
      ]),
      onPanResponderRelease: (e, {vx, vy}) => {
        this.state.pan.flattenOffset()
        Animated.spring(this.state.pan, {         //This will make the draggable card back to its original position
           toValue: 0
        }).start();
        this.setState({panResponder: undefined})   //Clear panResponder when user release on long press
      }
    })
  }

  _normalPanResponder(){
    return PanResponder.create({
      onPanResponderTerminationRequest: () => false,
      onStartShouldSetPanResponderCapture: () => true,
      onPanResponderGrant: (e, gestureState) => {
        this.state.pan.setOffset({x: this.state.pan.x._value, y: this.state.pan.y._value});
        this.state.pan.setValue({x: 0, y: 0})
        this.longPressTimer=setTimeout(this._onLongPress, 400)  // this is where you trigger the onlongpress panResponder handler
      },
      onPanResponderRelease: (e, {vx, vy}) => {
        if (!this.state.panResponder) {
            clearTimeout(this.longPressTimer);   // clean the timeout handler
        }
      }
    })
  }
Run Code Online (Sandbox Code Playgroud)

定义你的_onLongPress函数:

  _onLongPress(){
    // you can add some animation effect here as wll
    this.setState({panResponder: this._onLongPressPanResponder()})
  }
Run Code Online (Sandbox Code Playgroud)

定义你的构造函数:

  constructor(props){
    super(props)
    this.state = {
      pan: new Animated.ValueXY()
    };
    this._onLongPress = this._onLongPress.bind(this)
    this._onLongPressPanResponder = this._onLongPressPanResponder.bind(this)
    this._normalPanResponder = this._normalPanResponder.bind(this)
    this.longPressTimer = null
  }
Run Code Online (Sandbox Code Playgroud)

最后,在渲染之前,您应该根据状态切换到不同的 panResponder 处理程序:

  let panHandlers = {}
    if(this.state.panResponder){
      panHandlers = this.state.panResponder.panHandlers
    }else{
      panHandlers = this._normalPanResponder().panHandlers
    }
Run Code Online (Sandbox Code Playgroud)

然后将其附加panHandlers到您的视图中, {...panHandlers} 您甚至可以更改不同 panHandler 的 css 以显示不同的效果。