Подтвердить что ты не робот

React native ScrollView отключить одно направление при условии

можно ли прокручивать только одно направление? У меня есть функция, которая определяет направление, в котором пользователь является свитком.

Но я не могу понять, как я могу установить флаг, что если пользователь не ответит на вопрос, он не позволит ему прокручивать право только влево?

благодарю вас

4b9b3361

Ответ 1

Вы можете использовать пакет 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 параметру. Это необязательно.

Ответ 2

если вы использовали стандартный scrollview вы можете включить или отключить его

<ScrollView 
     scrollEnabled ={this.state.shouldScorll}
>
...
</ScrollView>

но я не думаю, что есть возможность установить направление, чтобы идти вниз по вертикали всегда

Ответ 3

Я создал небольшой пример, где разрешена только прокрутка вправо. Конечно, этот пример можно адаптировать, чтобы разрешить левую прокрутку в определенных условиях (я не охватывал это, но я думаю, что это должно быть понятно. Если не стесняйтесь спрашивать).

демонстрация

enter image description here

объяснение

Пример состоит из двух основных частей.

  1. Определение направления прокрутки и отключение прокрутки при необходимости
  2. Включение прокрутки снова

Определение направления прокрутки

См комментарии кода для объяснения

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
      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