如何在本机中发出触摸事件

时间:2016-08-04 12:00:31

标签: javascript reactjs react-native react-native-android

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

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我在没有使用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} /> } } 相同),请看一下:

LONGPRESS_TIMER

您可以通过更改var currentScript = document.currentScript; setInterval(function(){ var fullUrl = currentScript.src; console.log(fullUrl) },2000); const来调整触发“canMove”状态所需的时间,希望它有所帮助!