向上拖动ScrollView,然后在React Native中继续滚动

Dav*_*vid 22 mobile-application reactjs react-native react-native-scrollview

我想要什么(GIF)

这是关于我想要实现的(降低质量的)GIF。

在此处输入图片说明

我想要什么(文字)

我有一个scrollview位于屏幕一半的。

我想要的是将其scrollview向上拖动,然后在到达某个位置时将其发送drag touch eventscrollview自身,以便它可以继续滚动。

我的尝试

  • 将滚动视图全屏显示在前景中,并在其顶部添加半屏填充contentContainerStyle。效果很好,但是我无法单击它后面的视图。有没有一种方法可以单击滚动视图的空白区域
  • 我还尝试检测滚动位置,以便相应地向上移动视图

    <ScrollView onScroll={event => moveScrollViewUpIfNeeded(event)}>
        ...
    </ScrollView>
    
    Run Code Online (Sandbox Code Playgroud)

    但是当我在iOS模拟器上进行测试时无法正常工作。事件甚至没有触发。React Native Docs指出onScroll需要scrollEventThrottle工作。但是,scrollEventThrottle在iOS上可用。就我而言,我也想在Android上使用它。
    而且,如果我成功实现了这一点,那么我将面临另一个UI问题:向上拖动ScrollView时,如何在视图尚未到达所需位置时阻止它滚动?

因此,能否请您给我一些实现此目标的提示:)?

谢谢,

Tim*_*Tim 7

我尝试了几种选择。这是我的性能最佳的解决方案。当然,这只是一个可行的示例,您可能需要进一步优化它以完全满足您的需求。

演示:

演示Gif

说明:

基本思想是将地图始终保持在叠加层的背景中。叠加层是绝对定位的,包含两个视图。一个视图是透明的,我们仍然可以在其中查看和控制地图,另一个视图包含实际的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变为全屏。因此,我们听听onScrollScrollView 的方法。请参见下面的代码和注释:

  _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的scrollEventThrottleprop,我们可以控制该_onScroll方法的调用频率。(在这里您可能需要微调)

完整的代码和工作点心

https://snack.expo.io/@tim1717/rnmapwithoverlay