React-native-swiper: Detect left, right when swipe

Created on 15 Oct 2016  路  10Comments  路  Source: leecade/react-native-swiper

How can detect left or right when swipe.
Thanks.

Most helpful comment

I think i found a better solution. I Use the onScrollBeginDrag and the onScroll event. The onScrolBeginDrag fires before the onScroll event, so I save the offset of the slide in the onScrollBeginDrag and when the onScroll event is fired I compare the offset.

onScrollBeginDrag(e) {
    this.setState({ offset: e.nativeEvent.contentOffset.y });
  }

  onScroll(e) {
    const dragOffset = e.nativeEvent.contentOffset.y;
    const { offset, index } = this.state;
    this.setState({ loadingIndex: dragOffset > offset ? index + 1 : index - 1 });
  }

 render() {
    const { channels, navigator } = this.props;
    const { index, loadingIndex } = this.state;

    return (
            <Swiper
              horizontal={false}
              showsButtons={false}
              showsPagination={false}
              ref={(swiper) => { this.swiper = swiper; }}
              loadMinimal
              loadMinimalSize={0}
              loadMinimalLoader={
                <LivePlayerSpinner
                  cover={channels[loadingIndex].cover}
                  title={channels[loadingIndex]['current-schedule-program'].title}
                />
              }
              loop={false}
              onIndexChanged={this.onIndexChange}
              index={index}
              onScrollBeginDrag={this.onScrollBeginDrag}
              onScroll={this.onScroll}
              scrollEventThrottle={16}
            >
              { channels.map(channel => <VideoPlayer live key={channel['content-id']} content={channel} navigator={navigator} />) }
            </Swiper>
);

All 10 comments

I am also in search of catching which direction I swiped and currently which index I am at.

Hey, I read through the issues and found out that using a combination of onMomentumScrollEnd and onScrollBeginDrag props together might be a solution. You can look for the changes on the index field of the state and detect left/right swipe according to the increase and decrease of the intial index which is captured by onScrollBeginDrag.

I've found a trick that works to disable right / left swipping

In my case I wanted to disable right swipping in the case the user didn't complete the card, for left swipping you could use the same logic:

onMomentumScrollEnd={(e, state, context) => {
            if(state.index > this.currentIndex && this.currentIndex > 0) {
              this._swiper.scrollBy(-1)
            }
            this.currentIndex = state.index;
          }}

You get this:
xyzsqinnko

@tdurand this way doesn't look good. still see the next slide.
Anyway to stop the scrolling?

@Tom29 stopping won't work since onMomentumScrollEnd gets called after animation is complete.

I think i found a better solution. I Use the onScrollBeginDrag and the onScroll event. The onScrolBeginDrag fires before the onScroll event, so I save the offset of the slide in the onScrollBeginDrag and when the onScroll event is fired I compare the offset.

onScrollBeginDrag(e) {
    this.setState({ offset: e.nativeEvent.contentOffset.y });
  }

  onScroll(e) {
    const dragOffset = e.nativeEvent.contentOffset.y;
    const { offset, index } = this.state;
    this.setState({ loadingIndex: dragOffset > offset ? index + 1 : index - 1 });
  }

 render() {
    const { channels, navigator } = this.props;
    const { index, loadingIndex } = this.state;

    return (
            <Swiper
              horizontal={false}
              showsButtons={false}
              showsPagination={false}
              ref={(swiper) => { this.swiper = swiper; }}
              loadMinimal
              loadMinimalSize={0}
              loadMinimalLoader={
                <LivePlayerSpinner
                  cover={channels[loadingIndex].cover}
                  title={channels[loadingIndex]['current-schedule-program'].title}
                />
              }
              loop={false}
              onIndexChanged={this.onIndexChange}
              index={index}
              onScrollBeginDrag={this.onScrollBeginDrag}
              onScroll={this.onScroll}
              scrollEventThrottle={16}
            >
              { channels.map(channel => <VideoPlayer live key={channel['content-id']} content={channel} navigator={navigator} />) }
            </Swiper>
);

@acollazomayer onScroll doesn't work for me, how do you get that event?

@femiveys the onScroll events comes with the swiper component

That's strange. I cannot access them. In the latest version onScroll is never called for me. Maybe it is because the pages are WebViews

@acollazomayer same here onScroll doesn't work for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gwhite-dayspring picture gwhite-dayspring  路  3Comments

tokict picture tokict  路  3Comments

wrannaman picture wrannaman  路  3Comments

diegolmello picture diegolmello  路  3Comments

kliuj picture kliuj  路  3Comments