From c02914876734e0a1af0850565ce6990598a70422 Mon Sep 17 00:00:00 2001 From: Will Rodenbusch Date: Sat, 7 Mar 2020 00:03:23 -0600 Subject: [PATCH] terminated swipes must be handled --- CardStack.js | 105 ++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/CardStack.js b/CardStack.js index a35b472..98af470 100644 --- a/CardStack.js +++ b/CardStack.js @@ -66,63 +66,66 @@ class CardStack extends Component { this.state.drag.setValue({ x: (horizontalSwipe) ? gestureState.dx : 0, y: (verticalSwipe) ? gestureState.dy : 0 }); }, onPanResponderTerminationRequest: (evt, gestureState) => true, - onPanResponderRelease: (evt, gestureState) => { - this.props.onSwipeEnd(); - const currentTime = new Date().getTime(); - const swipeDuration = currentTime - this.state.touchStart; - const { - verticalThreshold, - horizontalThreshold, - disableTopSwipe, - disableLeftSwipe, - disableRightSwipe, - disableBottomSwipe, - } = this.props; - - if (((Math.abs(gestureState.dx) > horizontalThreshold) || - (Math.abs(gestureState.dx) > horizontalThreshold * 0.6 && - swipeDuration < 150) - ) && this.props.horizontalSwipe) { - - const swipeDirection = (gestureState.dx < 0) ? width * -1.5 : width * 1.5; - if (swipeDirection < 0 && !disableLeftSwipe) { - this._nextCard('left', swipeDirection, gestureState.dy, this.props.duration); - } - else if (swipeDirection > 0 && !disableRightSwipe) { - this._nextCard('right', swipeDirection, gestureState.dy, this.props.duration); - } - else { - this._resetCard(); - } - } else if (((Math.abs(gestureState.dy) > verticalThreshold) || - (Math.abs(gestureState.dy) > verticalThreshold * 0.8 && - swipeDuration < 150) - ) && this.props.verticalSwipe) { - - const swipeDirection = (gestureState.dy < 0) ? height * -1 : height; - if (swipeDirection < 0 && !disableTopSwipe) { - - this._nextCard('top', gestureState.dx, swipeDirection, this.props.duration); - } - else if (swipeDirection > 0 && !disableBottomSwipe) { - this._nextCard('bottom', gestureState.dx, swipeDirection, this.props.duration); - } - else { - this._resetCard(); - } - } - else { - this._resetCard(); - } - }, - onPanResponderTerminate: (evt, gestureState) => { - }, + onPanResponderRelease: this.didTerminateOrRelease, + onPanResponderTerminate: this.didTerminateOrRelease, onShouldBlockNativeResponder: (evt, gestureState) => { return true; }, }); } + + + didTerminateOrRelease = (evt, gestureState) => { + this.props.onSwipeEnd(); + const currentTime = new Date().getTime(); + const swipeDuration = currentTime - this.state.touchStart; + const { + verticalThreshold, + horizontalThreshold, + disableTopSwipe, + disableLeftSwipe, + disableRightSwipe, + disableBottomSwipe, + } = this.props; + + if (((Math.abs(gestureState.dx) > horizontalThreshold) || + (Math.abs(gestureState.dx) > horizontalThreshold * 0.6 && + swipeDuration < 150) + ) && this.props.horizontalSwipe) { + + const swipeDirection = (gestureState.dx < 0) ? width * -1.5 : width * 1.5; + if (swipeDirection < 0 && !disableLeftSwipe) { + this._nextCard('left', swipeDirection, gestureState.dy, this.props.duration); + } + else if (swipeDirection > 0 && !disableRightSwipe) { + this._nextCard('right', swipeDirection, gestureState.dy, this.props.duration); + } + else { + this._resetCard(); + } + } else if (((Math.abs(gestureState.dy) > verticalThreshold) || + (Math.abs(gestureState.dy) > verticalThreshold * 0.8 && + swipeDuration < 150) + ) && this.props.verticalSwipe) { + + const swipeDirection = (gestureState.dy < 0) ? height * -1 : height; + if (swipeDirection < 0 && !disableTopSwipe) { + + this._nextCard('top', gestureState.dx, swipeDirection, this.props.duration); + } + else if (swipeDirection > 0 && !disableBottomSwipe) { + this._nextCard('bottom', gestureState.dx, swipeDirection, this.props.duration); + } + else { + this._resetCard(); + } + } + else { + this._resetCard(); + } + }; + componentDidUpdate(prevProps) { if (typeof this.props.children === 'undefined') return; if (!this._isSameChildren(this.props.children, prevProps.children)) {