From 14299a2011285445062bea8bda0e0da6d2b08c95 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 7 Apr 2020 11:25:54 +0800 Subject: [PATCH] feat(Swipe): improve swipe threshold (#6003) --- src/swipe/index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/swipe/index.js b/src/swipe/index.js index d72b61b57..01873c39d 100644 --- a/src/swipe/index.js +++ b/src/swipe/index.js @@ -199,6 +199,7 @@ export default createComponent({ if (!this.touchable) return; this.clear(); + this.touchStartTime = Date.now(); this.touchStart(event); this.correctPosition(); }, @@ -217,23 +218,28 @@ export default createComponent({ onTouchEnd() { if (!this.touchable || !this.swiping) return; - if (this.delta && this.isCorrectDirection) { + const { size, delta } = this; + const duration = Date.now() - this.touchStartTime; + const speed = delta / duration; + const shouldSwipe = Math.abs(speed) > 0.25 || Math.abs(delta) > size / 2; + + if (shouldSwipe && this.isCorrectDirection) { const offset = this.vertical ? this.offsetY : this.offsetX; let pace = 0; if (this.loop) { - pace = offset > 0 ? (this.delta > 0 ? -1 : 1) : 0; + pace = offset > 0 ? (delta > 0 ? -1 : 1) : 0; } else { - pace = -Math[this.delta > 0 ? 'ceil' : 'floor']( - this.delta / this.size - ); + pace = -Math[delta > 0 ? 'ceil' : 'floor'](delta / size); } this.move({ pace, emitChange: true, }); + } else if (delta) { + this.move({ pace: 0 }); } this.swiping = false; @@ -280,12 +286,12 @@ export default createComponent({ // auto move first and last swipe in loop mode if (loop) { - if (children[0]) { + if (children[0] && targetOffset !== minOffset) { const outRightBound = targetOffset < minOffset; children[0].offset = outRightBound ? trackSize : 0; } - if (children[count - 1]) { + if (children[count - 1] && targetOffset !== 0) { const outLeftBound = targetOffset > 0; children[count - 1].offset = outLeftBound ? -trackSize : 0; }