react-native-swiper icon indicating copy to clipboard operation
react-native-swiper copied to clipboard

Detect left, right when swipe

Open anbkit opened this issue 10 years ago • 10 comments

How can detect left or right when swipe. Thanks.

anbkit avatar Oct 15 '16 07:10 anbkit

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

milkersarac avatar Oct 25 '16 14:10 milkersarac

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.

milkersarac avatar Oct 26 '16 08:10 milkersarac

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 avatar Feb 28 '17 14:02 tdurand

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

dzpt avatar Jun 13 '17 10:06 dzpt

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

edencakir avatar Sep 06 '17 09:09 edencakir

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 avatar Sep 26 '18 19:09 acollazomayer

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

femiveys avatar Jun 19 '19 09:06 femiveys

@femiveys the onScroll events comes with the swiper component

acollazomayer avatar Jun 19 '19 14:06 acollazomayer

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

femiveys avatar Jun 19 '19 14:06 femiveys

@acollazomayer same here onScroll doesn't work for me

jonathanmachado avatar Mar 24 '20 01:03 jonathanmachado