2

我正在尝试启用 panResponder 以拖放模式在屏幕上移动组件。longPress但是,必须通过此类元素上的 a 来启动此拖放操作。

longPress捕获事件,因此当panResponder启用时onStartShouldSetPanResponder => this.state.panEnabled我们需要再次按下。

我想触发一个原生事件,或者,在不再次按下的情况下激活拖动。是否可以重新发出原生事件?我们可以以任何方式将它传递给 panResponder 吗?

谢谢!

4

1 回答 1

0

我已经在不使用的情况下完成了这项工作longTouch,而只是使用了panResponder(虽然效果相同longTouch),请看一下:

import React, { Component } from 'react';

import {
    View,
    PanResponder
} from 'react-native';

const LONGPRESS_TIMER = 300;

class DraggableComponent extends Component {
    constructor() {
        super();

        this.timer = null;

        this.state = {
            canMove: false
        }

       this._panResponder = this.setUpPanResponder();

   }
   setUpPanResponder() {

       const self = this;

       return PanResponder.create({
           onStartShouldSetPanResponder: (evt, gestureState) => true,
           onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
           onMoveShouldSetPanResponder: (evt, gestureState) => true,
           onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
           onPanResponderGrant: (evt, gestureState) => {

               self.timer = setTimeout(() => {
                   self.setState({canMove: true});
               }, LONGPRESS_TIMER);

           },
           onPanResponderMove: (evt, gestureState) => {

               if(this.state.canMove) {
                   console.log('moving', gestureState); 
                   // code to move view here
               }

           },
           onPanResponderTerminationRequest: (evt, gestureState) => true,
           onPanResponderRelease: (evt, gestureState) => {

               clearTimeout(self.timer); 
               self.setState({canMove: false});

           }
       });
   }
   render() {

       return <View 
           style={{
               width: 100, 
               height: 100, 
               backgroundColor: this.state.canMove ? 'green' : 'blue'}} 
               {...this._panResponder.panHandlers} />

   }

}

您可以通过更改 const 来调整触发“canMove”状态所需的时间LONGPRESS_TIMER,希望对您有所帮助!

于 2016-08-04T13:51:52.047 回答