Hammer.js,防止“轻击”触发“两次轻击”

时间:2019-12-10 09:44:54

标签: hammer.js

我有以下设置

function hammerIt(elm) {
    hammertime = new Hammer(elm, {});
    hammertime.on('tap doubletap pan pinch panstart panend pinchend swipe swipeleft swiperight swipedown swipeup', function(ev) {

        // Doesn't work...
        var doubletap = hammertime.get('doubletap');
        var tap = hammertime.get('tap');
        doubletap.recognizeWith(tap);
        tap.requireFailure(doubletap);

        if (ev.type == 'tap') {
            doSomething();
        }

        if (ev.type == 'doubletap') { // This is never fired....
            doSomethingElse();
        }

        // etc.
}

当我双击该元素时,会触发tap和doubletap事件。我只希望双击,但以某种方式我的代码不起作用?是因为我使用的是on(events)和.get(“ event name”),而不是像https://hammerjs.github.io/require-failure/上的示例中那样的专用事件?还是我遗漏了其他东西?

1 个答案:

答案 0 :(得分:0)

谢谢约翰·史密斯,根据您的小提琴,我想出了一个可行的解决方案:

function hammerIt(elm) {
    hammertime = new Hammer.Manager(elm);

    var doubletap = new Hammer.Tap({event: 'doubletap', taps: 2});
    var tap = new Hammer.Tap({event: 'tap'});
    var pan = new Hammer.Pan();
    var pinch = new Hammer.Pinch({enable:true});
    var swipe = new Hammer.Swipe();

    hammertime.add([doubletap, tap, pan, pinch, swipe]);

    hammertime.get('tap').requireFailure('doubletap');

    hammertime.on('tap doubletap pan pinch panstart panend pinchend swipe swipeleft swiperight swipedown swipeup', function(ev) {
        if (ev.type == 'tap') {
            doSomething();
        }

        if (ev.type == 'doubletap') {
            doSomethingElse();
        }

        // etc.
    }
}

我认为问题出在使用“新Hammer(elem ...)”而不是“新Hammer.Manager(elem)”。这种方式我必须手动添加识别器,而不是使用默认设置,但至少如预期般运作:-)