feat(Swipe): improve swipe threshold (#6003)

This commit is contained in:
neverland 2020-04-07 11:25:54 +08:00 committed by GitHub
parent a72942f31a
commit 14299a2011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -199,6 +199,7 @@ export default createComponent({
if (!this.touchable) return; if (!this.touchable) return;
this.clear(); this.clear();
this.touchStartTime = Date.now();
this.touchStart(event); this.touchStart(event);
this.correctPosition(); this.correctPosition();
}, },
@ -217,23 +218,28 @@ export default createComponent({
onTouchEnd() { onTouchEnd() {
if (!this.touchable || !this.swiping) return; 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; const offset = this.vertical ? this.offsetY : this.offsetX;
let pace = 0; let pace = 0;
if (this.loop) { if (this.loop) {
pace = offset > 0 ? (this.delta > 0 ? -1 : 1) : 0; pace = offset > 0 ? (delta > 0 ? -1 : 1) : 0;
} else { } else {
pace = -Math[this.delta > 0 ? 'ceil' : 'floor']( pace = -Math[delta > 0 ? 'ceil' : 'floor'](delta / size);
this.delta / this.size
);
} }
this.move({ this.move({
pace, pace,
emitChange: true, emitChange: true,
}); });
} else if (delta) {
this.move({ pace: 0 });
} }
this.swiping = false; this.swiping = false;
@ -280,12 +286,12 @@ export default createComponent({
// auto move first and last swipe in loop mode // auto move first and last swipe in loop mode
if (loop) { if (loop) {
if (children[0]) { if (children[0] && targetOffset !== minOffset) {
const outRightBound = targetOffset < minOffset; const outRightBound = targetOffset < minOffset;
children[0].offset = outRightBound ? trackSize : 0; children[0].offset = outRightBound ? trackSize : 0;
} }
if (children[count - 1]) { if (children[count - 1] && targetOffset !== 0) {
const outLeftBound = targetOffset > 0; const outLeftBound = targetOffset > 0;
children[count - 1].offset = outLeftBound ? -trackSize : 0; children[count - 1].offset = outLeftBound ? -trackSize : 0;
} }