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

标签 reactjs react-native mobile-application react-native-scrollview

我想要什么(GIF)

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

enter image description here

我想要什么(文本)

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

我想要的是将 scrollview 向上拖动,然后当它到达某个位置时,将 drag touch 事件发送到 scrollview > 本身,这样它就可以继续滚动。

我的尝试

  • 将 ScrollView 置于前台全屏,并为其 contentContainerStyle 添加半屏 padding-top。它运行良好,但我无法单击它后面的 View 。有没有办法点击 ScrollView 的空白区域
  • 我还尝试检测滚动位置,以便相应地向上移动 View

    <ScrollView onScroll={event => moveScrollViewUpIfNeeded(event)}>
        ...
    </ScrollView>
    

    但是当我在iOS模拟器上测试它时不起作用。事件甚至没有被触发。 React Native Docs指出 onScroll 需要 scrollEventThrottle 才能工作。但是,scrollEventThrottle 只是 available on iOS 。就我而言,我也想在 Android 上使用它。
    如果我成功实现了这一点,我应该面临另一个 UI 问题:向上拖动 ScrollView 时,当 View 尚未到达所需位置时,如何防止它滚动?

那么,您能给我一些实现此目标的提示吗:)?

谢谢,

最佳答案

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

演示:

Demo Gif

说明:

基本思想是让 map 始终保持在叠加层的背景中。覆盖层是绝对定位的并且包含两个 View 。一个View是透明的,我们仍然可以看到和控制 map ,另一个View包含实际的ScrollView。诀窍是设置父叠加层的 pointerEvents="box-none" 并使用我们想要与 map 交互的 View 的 pointerEvents="none" 禁用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>

滚动功能:

如果我们向下滚动 ScrollView,我们想要缩小空 View,以便 ScrollView 变为全屏。因此我们监听 ScrollView 的 onScroll 方法。请参阅下面的代码和注释:

  _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});
      }
    }
  }

通过 ScrollView 的 scrollEventThrottle 属性,我们可以控制 _onScroll 方法的调用频率。 (这里你可能需要微调)

完整代码和工作小吃

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

关于reactjs - 向上拖动 ScrollView,然后在 React Native 中继续滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53523595/

相关文章:

reactjs - React Redux 刷新问题

javascript - 语义用户界面 react : How to set content load on pagination

react-native - react 原生中的 redux 与 this.state

ios - 如何在 Swift 2.3 的 ScrollView 中仅禁用缩小功能?

reactjs - 由于 popper js typescript 错误,React 应用程序构建失败

reactjs - 在菜单上选择创建的选项关闭/使用可创建的组件选择模糊

react-native - React-navigation 如何导航到 redux 中的路线

reactjs - 找不到模块 'minizlib'

android - 有没有办法让 <paper-toolbar> 使用 Polymer Starter Kit 占据屏幕的整个宽度?

javascript - 如何提示 iPhone/iPad 访问者安装 native 应用程序?