Skip to content

Commit

Permalink
Fix issue where tap was fired when touching and moving to a point and…
Browse files Browse the repository at this point in the history
… back to the origin. To fix it measure the deltaX and deltaY and only tap if deltaX and deltaY are smaller then 30.
  • Loading branch information
Tobias Sasse authored and tobibo committed Sep 16, 2013
1 parent 24c3482 commit 63829c7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

$(document).ready(function(){
var now, delta
var now, delta, deltaX = 0, deltaY = 0

$(document.body)
.bind('touchstart', function(e){
Expand All @@ -59,6 +59,8 @@
touch.y2 = e.touches[0].pageY
if (Math.abs(touch.x1 - touch.x2) > 10)
e.preventDefault()
deltaX += Math.abs(touch.x1 - touch.x2)
deltaY += Math.abs(touch.y1 - touch.y2)
})
.bind('touchend', function(e){
cancelLongTap()
Expand All @@ -75,33 +77,38 @@

// normal tap
else if ('last' in touch)

// delay by one tick so we can cancel the 'tap' event if 'scroll' fires
// ('tap' fires before 'scroll')
tapTimeout = setTimeout(function() {

// trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap')
event.cancelTouch = cancelAll
touch.el.trigger(event)

// trigger double tap immediately
if (touch.isDoubleTap) {
touch.el.trigger('doubleTap')
touch = {}
}

// trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function(){
touchTimeout = null
touch.el.trigger('singleTap')
// don't fire tap when delta position change by 30
// for instance when moving to a point and back to origin
if (deltaX < 30 && deltaY < 30) {
// delay by one tick so we can cancel the 'tap' event if 'scroll' fires
// ('tap' fires before 'scroll')
tapTimeout = setTimeout(function() {

// trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap')
event.cancelTouch = cancelAll
touch.el.trigger(event)

// trigger double tap immediately
if (touch.isDoubleTap) {
touch.el.trigger('doubleTap')
touch = {}
}, 250)
}

}, 0)
}

// trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function(){
touchTimeout = null
touch.el.trigger('singleTap')
touch = {}
}, 250)
}
}, 0)
} else {
touch = {}
}
deltaX = deltaY = 0

})
.bind('touchcancel', cancelAll)
Expand Down
26 changes: 26 additions & 0 deletions test/touch.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,37 @@ <h1>Touch tests</h1>
}, 50)
})
}, 50)
},

testTapDoNotFireWhenMoveToPointAndBack: function (t) {
var tapCount = 0, element = $('#test').get(0)

$('#test').on('tap', function(){
tapCount++
})
down(element, 10, 10)
move(element, 60, 10)

t.pause()
setTimeout(function(){
t.resume(function(){
move(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
t.resume( function () {
t.assertEqual(0, tapCount)
})
}, 100)
})
}, 100)
}

// TODO: test swipes in specific directions
})



})()
</script>
</body>
Expand Down

0 comments on commit 63829c7

Please sign in to comment.