Skip to content

Commit d858e54

Browse files
author
Matt Bryson
committed
Fixed bug with cancelThreshold introduced in 1.6.3, where swipe status no longer fired start event, and stopped once swiping back
1 parent 665b99b commit d858e54

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

demos/Thresholds.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,19 @@
6161
},
6262
swipeStatus:function(event, phase, direction, distance, duration, fingers) {
6363

64-
$("#test3").text("Your have swiped " + distance + " px so far");
64+
$("#test3").text(" Your have swiped " + distance + " px so far");
6565

6666
if(distance>200) {
67-
$("#test3").text("Now swipe back 50px and release to cancel.. distance = " + distance + "px");
67+
$("#test3").text(" Now swipe back 10px and release to cancel.. distance = " + distance + "px");
6868
}
6969

7070
if (phase=="cancel") {
71-
$("#test3").text("You cancelled the swipe");
71+
$("#test3").text(" You cancelled the swipe");
7272
}
7373
},
7474
threshold:200,
7575
cancelThreshold:10
76+
7677
});
7778
});
7879
</script>

jquery.touchSwipe.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
* - added option method to update init options at runtime
7878
*
7979
* $version 1.6.3 - added doubletap, longtap events and longTapThreshold, doubleTapThreshold property
80+
* $Date: 2013-04-04 (Thurs, 04 April 2013) $
81+
* $version 1.6.4 - Fixed bug with cancelThreshold introduced in 1.6.3, where swipe status no longer fired start event, and stopped once swiping back.
8082
*/
8183

8284
/**
@@ -143,7 +145,7 @@
143145
* @namespace
144146
* @property {int} [fingers=1] The number of fingers to detect in a swipe. Any swipes that do not meet this requirement will NOT trigger swipe handlers.
145147
* @property {int} [threshold=75] The number of pixels that the user must move their finger by before it is considered a swipe.
146-
* @property {int} [cancelThreshold=25] The number of pixels that the user must move their finger back from the original swipe direction to cancel the gesture.
148+
* @property {int} [cancelThreshold=null] The number of pixels that the user must move their finger back from the original swipe direction to cancel the gesture.
147149
* @property {int} [pinchThreshold=20] The number of pixels that the user must pinch their finger by before it is considered a pinch.
148150
* @property {int} [maxTimeThreshold=null] Time, in milliseconds, between touchStart and touchEnd must NOT exceed in order to be considered a swipe.
149151
* @property {int} [fingerReleaseThreshold=250] Time in milliseconds between releasing multiple fingers. If 2 fingers are down, and are released one after the other, if they are within this threshold, it counts as a simultaneous release.
@@ -175,7 +177,7 @@
175177
var defaults = {
176178
fingers: 1,
177179
threshold: 75,
178-
cancelThreshold:25,
180+
cancelThreshold:null,
179181
pinchThreshold:20,
180182
maxTimeThreshold: null,
181183
fingerReleaseThreshold:250,
@@ -722,7 +724,10 @@
722724
duration = calculateDuration();
723725

724726
//If we trigger handlers at end of swipe OR, we trigger during, but they didnt trigger and we are still in the move phase
725-
if (options.triggerOnTouchEnd || (options.triggerOnTouchEnd == false && phase === PHASE_MOVE)) {
727+
if(didSwipeBackToCancel()) {
728+
phase = PHASE_CANCEL;
729+
triggerHandler(event, phase);
730+
} else if (options.triggerOnTouchEnd || (options.triggerOnTouchEnd == false && phase === PHASE_MOVE)) {
726731
phase = PHASE_END;
727732
triggerHandler(event, phase);
728733
}
@@ -810,10 +815,10 @@
810815
// Ensure we have valid swipe (under time and over distance and check if we are out of bound...)
811816
var validTime = validateSwipeTime();
812817
var validDistance = validateSwipeDistance();
813-
818+
var didCancel = didSwipeBackToCancel();
814819

815820
//If we have exceeded our time, then cancel
816-
if(!validTime) {
821+
if(!validTime || didCancel) {
817822
nextPhase = PHASE_CANCEL;
818823
}
819824
//Else if we are moving, and have reached distance then end
@@ -825,7 +830,6 @@
825830
nextPhase = PHASE_CANCEL;
826831
}
827832

828-
829833
return nextPhase;
830834
}
831835

@@ -842,13 +846,13 @@
842846
var ret = undefined;
843847

844848
// SWIPE GESTURES
845-
if(didSwipe()) {
849+
if(didSwipe() || hasSwipes()) { //hasSwipes as status needs to fire even if swipe is invalid
846850
//Trigger the swipe events...
847851
ret = triggerHandlerForGesture(event, phase, SWIPE);
848-
}
852+
}
849853

850854
// PINCH GESTURES (if the above didnt cancel)
851-
else if(didPinch() && ret!==false) {
855+
else if((didPinch() || hasPinches()) && ret!==false) {
852856
//Trigger the pinch events...
853857
ret = triggerHandlerForGesture(event, phase, PINCH);
854858
}
@@ -1121,12 +1125,22 @@
11211125
valid = distance >= options.threshold;
11221126
}
11231127

1124-
//And we didn't swipe back to cancel...
1125-
if(valid && options.cancelThreshold !== null) {
1126-
valid = (getMaxDistance( direction ) - distance) < options.cancelThreshold;
1128+
return valid;
1129+
}
1130+
1131+
/**
1132+
* Checks the user has swiped back to cancel.
1133+
* @return Boolean if <code>cancelThreshold</code> has been set, return true if the cancelThreshold was met, else false.
1134+
* If no cancelThreshold was set, then we return true.
1135+
* @inner
1136+
*/
1137+
function didSwipeBackToCancel() {
1138+
var cancelled = false;
1139+
if(options.cancelThreshold !== null && direction !==null) {
1140+
cancelled = (getMaxDistance( direction ) - distance) >= options.cancelThreshold;
11271141
}
11281142

1129-
return valid;
1143+
return cancelled;
11301144
}
11311145

11321146
/**
@@ -1256,13 +1270,14 @@
12561270
function validateSwipe() {
12571271
//Check validity of swipe
12581272
var hasValidTime = validateSwipeTime();
1259-
var hasValidDistance = validateSwipeDistance();
1260-
var hasCorrectFingerCount = validateFingers();
1273+
var hasValidDistance = validateSwipeDistance();
1274+
var hasCorrectFingerCount = validateFingers();
12611275
var hasEndPoint = validateEndPoint();
1276+
var didCancel = didSwipeBackToCancel();
12621277

12631278
// if the user swiped more than the minimum length, perform the appropriate action
12641279
// hasValidDistance is null when no distance is set
1265-
var valid = hasEndPoint && hasCorrectFingerCount && hasValidDistance && hasValidTime;
1280+
var valid = !didCancel && hasEndPoint && hasCorrectFingerCount && hasValidDistance && hasValidTime;
12661281

12671282
return valid;
12681283
}

0 commit comments

Comments
 (0)