React native React native ScrollView在条件下禁用一个方向

React native React native ScrollView在条件下禁用一个方向,react-native,react-native-android,react-native-ios,react-native-scrollview,React Native,React Native Android,React Native Ios,React Native Scrollview,是否可以只滚动到一个方向? 我有一个功能,检测方向的用户是一个滚动 但是我不知道如何设置一个标志,如果用户不回答这个问题,它将不允许他向右滚动,只允许向左滚动 谢谢您可以使用react native directed scrollview软件包 包装: 这个包有scrollTo({x:100,y:100,animated:true})方法。 如果您将x轴坐标限制为您的条件,则可以 试试这样的 if(isAnswered == false){ scrollTo({x: /*x-axis po

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

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


谢谢

您可以使用
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
参数。它是可选的。

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

演示

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

  • 检测滚动方向,必要时禁用滚动
  • 再次启用滚动
  • 检测滚动方向

      <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>
    
    有关说明,请参见代码注释

    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 });
          }
        }
    
    再次启用滚动:

      <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>
    
    我们正在使用计时器在指定的时间后再次启用滚动

    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>
    
    
    this.handleScroll(事件)}
    ref={view=>this.\u scrollview=view}
    >
    
    工作示例

    directionalLockEnabled-这对您有用吗?-仅在IOS上,无需使用其他软件包。该功能可用于标准的
    滚动视图