【问题标题】:JS multitouch for checking swipes用于检查滑动的 JS 多点触控
【发布时间】:2013-09-11 14:27:51
【问题描述】:

我一直在寻找解决方案以找出在移动浏览器上左右滑动并创建 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;

【问题讨论】:

    标签: javascript android ios multi-touch touchstart


    【解决方案1】:

    你有一个叫做 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

    【讨论】:

    • 谢谢你,但我不需要手势。使用手势库就像使用大炮杀死苍蝇一样。我只需要简单地检测左右滑动即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多