Dav*_*vid 22 mobile-application reactjs react-native react-native-scrollview
这是关于我想要实现的(降低质量的)GIF。
我有一个scrollview
位于屏幕一半的。
我想要的是将其scrollview
向上拖动,然后在到达某个位置时将其发送drag touch event
给scrollview
自身,以便它可以继续滚动。
contentContainerStyle
。效果很好,但是我无法单击它后面的视图。有没有一种方法可以单击滚动视图的空白区域?我还尝试检测滚动位置,以便相应地向上移动视图
<ScrollView onScroll={event => moveScrollViewUpIfNeeded(event)}>
...
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
但是当我在iOS模拟器上进行测试时无法正常工作。事件甚至没有触发。React Native Docs指出onScroll
需要scrollEventThrottle
工作。但是,scrollEventThrottle
仅在iOS上可用。就我而言,我也想在Android上使用它。
而且,如果我成功实现了这一点,那么我将面临另一个UI问题:向上拖动ScrollView时,如何在视图尚未到达所需位置时阻止它滚动?
因此,能否请您给我一些实现此目标的提示:)?
谢谢,
我尝试了几种选择。这是我的性能最佳的解决方案。当然,这只是一个可行的示例,您可能需要进一步优化它以完全满足您的需求。
基本思想是将地图始终保持在叠加层的背景中。叠加层是绝对定位的,包含两个视图。一个视图是透明的,我们仍然可以在其中查看和控制地图,另一个视图包含实际的ScrollView。诀窍是设置pointerEvents="box-none"
父叠加层,并pointerEvents="none"
在要与地图进行交互的View上禁用pointerEvents 。
基本渲染方法:
<SafeAreaView style={{flex: 1}}>
<MapView
initialRegion={region}
style={{height: HEIGHT, width: WIDTH}}
/>
<View pointerEvents="box-none"style={{height: HEIGHT, width: WIDTH, position: 'absolute'}}>
<View pointerEvents="none" style={{height: this.state.height, backgroundColor: 'transparent'}} />
<View style={{ height: HEIGHT-this.state.height, backgroundColor: 'white'}}>
<ScrollView onScroll={(e) => this._onScroll(e)} scrollEventThrottle={10} >
-- content of scrollview goes here ---
</ScrollView>
</View>
</View>
</SafeAreaView>
Run Code Online (Sandbox Code Playgroud)
滚动功能:
如果向下滚动ScrollView,则要缩小空的View,以便ScrollView变为全屏。因此,我们听听onScroll
ScrollView 的方法。请参见下面的代码和注释:
_onScroll(e){
// from the nativeEvent we can get the contentOffsett
var offset_y = e.nativeEvent.contentOffset.y;
if (offset_y > 0 ) {
if (this.state.height>=0){
// we are scrolling down the list, decrease height of the empty view
this.setState({height: this.state.height-offset_y});
}
}
if (offset_y <0){
if (this.state.height <= this.state.mapHeight){
// we are scrolling up the list, increase size of empty view/map view
this.setState({height: this.state.height-offset_y});
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用ScrollView的scrollEventThrottle
prop,我们可以控制该_onScroll
方法的调用频率。(在这里您可能需要微调)
https://snack.expo.io/@tim1717/rnmapwithoverlay
归档时间: |
|
查看次数: |
1034 次 |
最近记录: |