From 4d63523ae351b7e434abd43556cc4ad3eed0644e Mon Sep 17 00:00:00 2001 From: Timak304 Date: Tue, 13 May 2014 09:39:25 +0200 Subject: [PATCH 1/2] Invalidate longpress on move and add startPoint data. --- README.md | 5 ++++- jquery.touchy.js | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5d259f1..503ac43 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,10 @@ However, there are exceptions to this pattern. Please see below. ### touchy-longpress phase: "start" or "end" -(No data object) + +data: + +* startPoint ### touchy-drag diff --git a/jquery.touchy.js b/jquery.touchy.js index 21fd041..55fec3f 100644 --- a/jquery.touchy.js +++ b/jquery.touchy.js @@ -48,9 +48,10 @@ msThresh: 800, triggerStartPhase: false, data: { - startDate: null + startDate: null, + startPoint: null }, - proxyEvents: ["TouchStart", "TouchEnd"] + proxyEvents: ["TouchStart", "TouchEnd", "TouchMove"] }, drag: { // the user touches the element and then moves his or her finger across the screen preventDefault: { @@ -175,10 +176,10 @@ }; data.startDate = e.timeStamp; if (settings.triggerStartPhase) { - $target.trigger('touchy-longpress', ['start', $target]); + $target.trigger('touchy-longpress', ['start', $target, data]); } data.timer = setTimeout($.proxy(function(){ - $target.trigger('touchy-longpress', ['end', $target]); + $target.trigger('touchy-longpress', ['end', $target, data]); }, this), settings.msThresh); break; @@ -293,6 +294,18 @@ } break; + //////////////// LONG PRESS //////////////// + case 'longpress': + var distance = Math.sqrt( Math.pow( (touches[0].pageX - data.startPoint.x), 2 ) + Math.pow( (touches[0].pageY - data.startPoint.y), 2 ) ); + if (distance > 2) { + // Invalidate the longpress + clearTimeout(data.timer); + $.extend(data, { + "startDate":null + }); + } + break; + //////////////// ROTATE //////////////// case 'rotate': var lastMovePoint, From 545feb3d8eecb7be947aad4f3bf3675d245aa021 Mon Sep 17 00:00:00 2001 From: Timak304 Date: Tue, 20 May 2014 10:29:27 +0200 Subject: [PATCH 2/2] Make longpress move tolerance configurable --- README.md | 1 + jquery.touchy.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 503ac43..8e54594 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ As shown in the example, the settings are accessed through the camelCased name o * requiredTouches: 1 * msThresh: 800 (the number of milliseconds the user must touch the element before the event is fired) * triggerStartPhase: false (whether to trigger the event during the start phase) +* moveTolerance: 2 (distance in pixel allowed during the delay, use negative value for infinite distance) ### touchy-drag diff --git a/jquery.touchy.js b/jquery.touchy.js index 55fec3f..12d5484 100644 --- a/jquery.touchy.js +++ b/jquery.touchy.js @@ -47,6 +47,7 @@ requiredTouches: 1, msThresh: 800, triggerStartPhase: false, + moveTolerance: 2, data: { startDate: null, startPoint: null @@ -296,13 +297,15 @@ //////////////// LONG PRESS //////////////// case 'longpress': - var distance = Math.sqrt( Math.pow( (touches[0].pageX - data.startPoint.x), 2 ) + Math.pow( (touches[0].pageY - data.startPoint.y), 2 ) ); - if (distance > 2) { - // Invalidate the longpress - clearTimeout(data.timer); - $.extend(data, { - "startDate":null - }); + if (settings.moveTolerance >= 0) { + var distance = Math.sqrt( Math.pow( (touches[0].pageX - data.startPoint.x), 2 ) + Math.pow( (touches[0].pageY - data.startPoint.y), 2 ) ); + if (settings.moveTolerance == 0 || distance > settings.moveTolerance) { + // Invalidate the longpress + clearTimeout(data.timer); + $.extend(data, { + "startDate":null + }); + } } break;