I have created a small example, where only scrolling to the right is allowed. Of course this example can be adapted to allow left scrolling in specific conditions. In the code, I marked the position where to add such a condition.
Demo
Explanation
The example consists of two main parts.
- Detecting the scroll direction and disable scroll if necessary
- Enabling the scroll again
Detecting scroll direction
See code comments for explanation
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 });
}
}
Enabling scroll again:
We are making use of a timer to enable scroll again after a specified amount of time.
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});
}
}
Render:
<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>
Working Example
https://snack.expo.io/rJAamRC2E