Skip to content

Commit

Permalink
fixing bugs with swipe and drag
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjamesdavies committed Apr 11, 2012
1 parent c2e021d commit bfe93ac
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 54 deletions.
4 changes: 2 additions & 2 deletions index-dev.html
Expand Up @@ -62,13 +62,13 @@ <h1>Touch Framework experiments</h1>
logger.appendChild(element);
}

touchLayer.unbind(id);
//touchLayer.unbind(id);
};

touchLayer.bind('tapstart', target, callback);
touchLayer.bind('tapcancel', target, callback);

id = touchLayer.bind('tap', target, callback);
touchLayer.bind('tap', target, callback);

touchLayer.bind('taphold', target, callback);
touchLayer.bind('doubletap', target, callback);
Expand Down
12 changes: 7 additions & 5 deletions js/DragController.js
Expand Up @@ -41,16 +41,16 @@ var TouchLayer_DragController = function (options) {
if (e.originalEvent) {
e = e.originalEvent;
}

if (preventDefault) {
e.preventDefault();
e.stopPropagation();
}
if (e.touches) {

if (e.touches !== undefined) {
startX = previousX = e.touches[0].pageX;
startY = previousY = e.touches[0].pageY;
} else if (e.startX && e.startY) {
} else if (e.pageX && e.pageY) {
startX = previousX = e.pageX;
startY = previousY = e.pageY;
}
Expand All @@ -61,11 +61,12 @@ var TouchLayer_DragController = function (options) {
};

var onTouchMove = function (e) {

if (startX && startY) {
if (e.originalEvent) {
e = e.originalEvent;
}

if (preventDefault) {
e.preventDefault();
e.stopPropagation();
Expand Down Expand Up @@ -140,6 +141,7 @@ var TouchLayer_DragController = function (options) {
}
dragging = false;
touchStart = false;
startX = startY = null;
};

mainController.bind(options.el, 'touchstart', onTouchStart, runOnBubble);
Expand Down
102 changes: 56 additions & 46 deletions js/SwipeController.js
Expand Up @@ -49,65 +49,75 @@ var TouchLayer_SwipeController = function (options) {
};

var onTouchMove = function (e) {
if (e.originalEvent) {
e = e.originalEvent;
}

if (preventDefault) {
e.preventDefault();
e.stopPropagation();
}

var deltaX = null,
deltaY = null;
if (e.touches) { //e.pageX / pageY give 0 values in android
deltaY = e.touches[0].pageY - startY;
deltaX = e.touches[0].pageX - startX;
} else if (e.pageX && e.pageY) {
deltaY = e.pageY - startY;
deltaX = e.pageX - startX;
}

if (deltaX && deltaY) {
var absDeltaY = Math.abs(deltaY),
absDeltaX = Math.abs(deltaX),
deltaTime = e.timeStamp - startTime;

// If the swipeTime is over, we are not gonna check for it again
if (absDeltaY - absDeltaX > 3 || deltaTime > swipeTime) {
stopped = true;

} else if (absDeltaX > swipeThreshold && absDeltaX > absDeltaY) {
// If this is a swipe, a scroll is not possible anymore
var info = {
direction: (deltaX < 0) ? 'left' : 'right',
distance: absDeltaX,
deltaTime: deltaTime,
deltaX: deltaX,
deltaY: deltaY
};

if (options.eventName === 'swipe') {
var data = mainController.makeReturnData(e, options.el, info);
mainController.fire('swipe', options.callback, data);

if (preventDefault) {
e.preventDefault();
e.stopPropagation();

if (startX && startY) {
if (e.originalEvent) {
e = e.originalEvent;
}

if (preventDefault) {
e.preventDefault();
e.stopPropagation();
}

var deltaX = null,
deltaY = null;
if (e.touches) { //e.pageX / pageY give 0 values in android
deltaY = e.touches[0].pageY - startY;
deltaX = e.touches[0].pageX - startX;
} else if (e.pageX && e.pageY) {
deltaY = e.pageY - startY;
deltaX = e.pageX - startX;
}

if (deltaX && deltaY) {
var absDeltaY = Math.abs(deltaY),
absDeltaX = Math.abs(deltaX),
deltaTime = e.timeStamp - startTime;

// If the swipeTime is over, we are not gonna check for it again
if (absDeltaY - absDeltaX > 3 || deltaTime > swipeTime) {
stopped = true;

} else if (absDeltaX > swipeThreshold && absDeltaX > absDeltaY) {
// If this is a swipe, a scroll is not possible anymore
var info = {
direction: (deltaX < 0) ? 'left' : 'right',
distance: absDeltaX,
deltaTime: deltaTime,
deltaX: deltaX,
deltaY: deltaY
};

if (options.eventName === 'swipe') {
var data = mainController.makeReturnData(e, options.el, info);
mainController.fire('swipe', options.callback, data);

if (preventDefault) {
e.preventDefault();
e.stopPropagation();
}
}
stopped = true;
}
stopped = true;
}
}
};

var onTouchEnd = function () {
stopped = true;
startX = startY = null;
};

mainController.bind(options.el, 'touchstart', onTouchStart, runOnBubble);
mainController.bind(options.el, 'touchmove', onTouchMove, runOnBubble);
mainController.bind(options.el, 'touchend', onTouchEnd, runOnBubble);

return {
unbind: function () {
mainController.unbind(options.el, 'touchstart', onTouchStart);
mainController.unbind(options.el, 'touchmove', onTouchMove);
mainController.unbind(options.el, 'touchend', onTouchEnd);
}
};
};
2 changes: 1 addition & 1 deletion jsTouchLayer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bfe93ac

Please sign in to comment.