【问题标题】:React native ScrollView disable one direction on conditionReact native ScrollView 在条件下禁用一个方向
【发布时间】:2018-03-29 06:18:45
【问题描述】:

是否可以只滚动到一个方向? 我有一个功能可以检测用户滚动的方向。

但我不知道如何设置一个标志,如果用户不回答问题,它将不允许他向右滚动只向左滚动?

谢谢

【问题讨论】:

标签: react-native react-native-android react-native-ios react-native-scrollview


【解决方案1】:

我创建了一个小例子,只允许向右滚动。当然,这个例子可以适应在特定条件下允许左滚动。在代码中,我标记了添加此类条件的位置。

演示

说明

该示例由两个主要部分组成。

  1. 检测滚动方向并在必要时禁用滚动
  2. 再次启用滚动

检测滚动方向

解释见代码cmets

handleScroll(event){
      // WIDTH originates from Dimensions.get('screen').width
      const endOfView = event.nativeEvent.contentSize.width - WIDTH; 
      const positionX = event.nativeEvent.contentOffset.x; 
      const positionY = event.nativeEvent.contentOffset.y; 

      // check if we are scrolling left, also detect if we are at the end of the scrollview 
      // MARKED: check other conditions here to allow scrolling again
      if(this.state.lastPositionX > positionX && endOfView > positionX){
        // we are scrolling left, disable scroll, reset the current position
        this.setState({ lastPositionX: positionX, lastPositionY: positionY, allowScroll: false });
        // scroll back to last valid position. Important! Otherwise users may be able to scroll left
        this._scrollview.scrollTo({x: this.state.lastPositionX, y: this.state.lastPositionY});
        //call the timer to enable scroll again 
        this.callTimer();
      }else{
        // we are scrolling right, everthing is fine
        this.setState({ lastPositionX: positionX, lastPositionY: positionY });
      }
    }

再次启用滚动:

我们正在使用计时器在指定的时间后再次启用滚动。

timerFn() {
  // clear the timer again, otherwise the timer will fire over and over again 
  clearInterval(this.state.timer);
  //enable scroll and reset timer 
  this.setState({allowScroll: true, timer: null });

}
callTimer() {
  if (this.state.timer == null ){ 
    // no timer is available, we create a new one. Maybe you have to fine tune the duration 
    let timer = setInterval(() => this.timerFn(), 1000);
    this.setState({timer});
  }
}

渲染:

  <SafeAreaView style={styles.container}>
   <ScrollView
   horizontal
   scrollEventThrottle={15}
   scrollEnabled={this.state.allowScroll}
   onScroll={(event) => this.handleScroll(event)}
   ref={view => this._scrollview = view}
   >
     <View style={{width: WIDTH, backgroundColor: 'red'}} />
     <View style={{width: WIDTH, backgroundColor: 'green'}} />
     <View style={{width: WIDTH, backgroundColor: 'blue'}} />
   </ScrollView>
  </SafeAreaView>

工作示例

https://snack.expo.io/rJAamRC2E

【讨论】:

    【解决方案2】:

    你可以使用react-native-directed-scrollview

    包装: https://www.npmjs.com/package/react-native-directed-scrollview

    这个包有scrollTo({x: 100, y: 100, animated: true}) 方法。 如果您将 x 轴坐标限制在您的条件下,就可以了。

    所以试试这样的,

    if(isAnswered == false){
      scrollTo({x: /*x-axis position*/, y: /*y-axis position*/, animated: true})
    }
    

    注意:您可以将false 传递给animated 参数。它是可选的。

    【讨论】:

    • 无需使用额外的包。 scrollTo 函数可用于标准 ScrollView
    【解决方案3】:

    对于任何想要具有隐藏内容的固定高度容器但需要ScrollViewRefreshControl 功能的人,我所做的是使用ScrollView 但固定其高度。

    当我想显示其他内容时,我会为每个内容块的高度/不透明度设置动画以产生滚动效果。

    <ScrollView
        refreshControl={...}
        style={{ height: contentBlockHeight, overflow: 'hidden' }}
        contentContainerStyle={{ height: contentBlockHeight, overflow: 'hidden' }}
    >
    ...
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      相关资源
      最近更新 更多