我不确定这是否是完美的解决方案,但您可以做的是创建一个 EvenEmitter 并让 Webview 捕获所有点击。应该由地图处理的,我可以发送到地图本身。
我在屏幕上使用它们,可能会创建多个组件,但可以将它们发送到父屏幕。它主要用于处理模态屏幕。
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
this._eventEmitter = new EventEmitter();
this._eventEmitter.addListener('modal', (data) => {
this.setState({
visibleModal: true,
modalData: data
});
});
然后你可以像emitter.emit("modal", data)一样使用它们
编辑:
这是另一种方法,它还向您展示了如何使用您在问题中提出的方法。
class Class extends Component {
onStartShouldSetResponder = () => true;
onEvent = (e) => {
let object = {};
object.locationX = e.nativeEvent.locationX;
object.locationY = e.nativeEvent.locationY;
object.pageY = e.nativeEvent.pageY;
object.pageX = e.nativeEvent.pageX;
object.target = e.nativeEvent.target;
//object.touches = e.nativeEvent.touches; //These will trigger a cyclic error while parsing but you can access them
//object.changedTouches = e.nativeEvent.changedTouches;
object.identifier = e.nativeEvent.identifier;
alert(JSON.stringify(object));
if (object.pageX > this.state.x && object.pageX < (this.state.x + this.state.width) && object.pageY > this.state.y && object.pageY < (this.state.y + this.state.height))
alert("inside") //Here you can trigger whatever function you need from the underlying view
}
onLayout = (event) => {
this.setState({
x: event.nativeEvent.layout.x,
y: event.nativeEvent.layout.y,
width: event.nativeEvent.layout.width,
height: event.nativeEvent.layout.height,
});
}
render(){
return(
<ScrollView>
<View ref={"target"} style={{
position: 'absolute',
height: 200,
width: 200,
left: 100,
top: 100,
backgroundColor: 'rgba(0,0,0,0.5)'
}}
onLayout={this.onLayout}
/>
<View
onl
onStartShouldSetResponder={this.onStartShouldSetResponder}
onResponderMove={this.onEvent}
style={{
backgroundColor: 'rgba(255,0,0,0.3)',
width: '100%',
height: 150
}}/>
<TouchableWithoutFeedback
onPress={this.onEvent}
onLongPress={this.onEvent}
style={{
backgroundColor: 'rgba(0,255,0,0.3)',
width: '100%',
height: 150
}}>
<View style={{height: 150, backgroundColor: 'rgba(0,255,0,0.3)'}}/>
</TouchableWithoutFeedback>
<View
onTouchStart={this.onEvent}
onTouchEnd={this.onEvent}
style={{
backgroundColor: 'rgba(0,0,255,0.3)',
width: '100%',
height: 150
}}>
</View>
</ScrollView>
);
}
}
也许这会帮助您将事件传递给其他视图