0

我一直在寻找解决方案来找出在移动浏览器上左右滑动并创建 lib(下)。我的问题是:有没有一种简单的方法来检查 android 和 iOS 上的滑动?另外:我可以改用鼠标事件吗?

(function (lib, img, cjs) {
lib.touches = new Array();
(lib.setTouchListeners = function(gFilename) {
    document.addEventListener("touchstart", function(event) {
        // index to the last touch (always the last in the list)
        var index = event.touches.length - 1;
        // identifier - unique id of the touch (always the least free number on android but quite large number on iOS)
        var iden = event.touches[index].identifier;
        // save this touch for later comparison (checking if it moved etc.)
        var t = {pageX:event.touches[index].pageX, pageY:event.touches[index].pageY, identifier:event.touches[index].identifier};
        lib.touches.push(t);
    },false);
    document.addEventListener("touchmove", function(event) {
        // prevent browser from using touch (move page up and down, resize by pinch)
        event.preventDefault();
    },false);
    document.addEventListener("touchend", function(event) {
        var len = event.changedTouches.length;
        var iden = 0;
        // check all changes although it's always one (but just in case something changes in future)
        for(var i = 0; i < len; i++) {
            iden = event.changedTouches[i].identifier;
            // find that identifier in the saved list
            for(var j = 0; j < lib.touches.length; j++) {
                if (iden == lib.touches[j].identifier) {
                    var horizontalMove = event.changedTouches[i].pageX - lib.touches[j].pageX;
                    var verticalMove = event.changedTouches[i].pageY - lib.touches[j].pageY;
                    if (Math.abs(horizontalMove) > 50) {
                        if (horizontalMove > 0) {
                            console.log('right');
                        } else {
                            console.log('left');
                        }
                    }
                    if (Math.abs(verticalMove) > 50) {
                        if (verticalMove > 0) {
                            console.log('down');
                        } else {
                            console.log('up');
                        }
                    }
                    lib.touches.splice(j, 1);
                    break;
                }
            }
        }
//      event.preventDefault();
    },false);

});
})(lib = lib||{}, images = images||{}, createjs = createjs||{});
var lib, images, createjs;
4

1 回答 1

0

你有一个叫做 GestureOverlay 或 GestureDetector 的东西,你可以用它来检查滑动。以下是 GestureOverlay 和 GestureDetector 的站点:

http://developer.android.com/reference/android/gesture/GestureOverlayView.html http://developer.android.com/reference/android/view/GestureDetector.html

看我的另一个答案:

https://stackoverflow.com/a/18737199/2767703

于 2013-09-11T14:31:45.843 回答