禁用除单击之外的所有hammer.js(角锤)事件

时间:2014-11-26 00:09:06

标签: javascript ios cordova touch hammer.js

我的应用使用angular-hammer,我想仅针对点击事件对其进行优化,以获得最佳性能,因为我不需要任何其他手势。我理解锤子只会为指定的hm-*选项添加听众,但我想明确阻止它监听其他事件(特别是双击)。我不知道这是否有必要。 (该应用程序在按钮按下非常密集,我已经了解到这是iOS和Android混合开发的弱点;我希望能够做到正确。)

我正在使用的论点如下,我在注释中的解释。这是一个好的做法,我能做的最好吗?

// do not propagate further events (like pan or swipe) from clicking this element
hm-manager-options="{'touchAction':'none'}"   

// disable double tap so it knows any tap is a single tap 
hm-recognizer-options="[
    {'type':'tap','event':'tap'}, 
    {'type':'dbltap','enabled':'false'}
]" 

1 个答案:

答案 0 :(得分:-1)

我知道这是一个非常古老的问题,但如果您只对Tap事件感兴趣,那么为什么不使用ng-click?这是一次点击(或鼠标点击)。

听起来你真的可能根本不需要Hammer。如果您只需要一个简单的点击事件处理程序,则可以使用Bob Nisco's onLongPress directive。你只需要删除一些位来获得你可能正在寻找的触敏事件。

Here's a Plunk that has an onTap directive基于Bob Nisco的工作。仅当存在触摸事件时才会触发指定的处理程序 - 不是鼠标单击。这是应用程序,控制器和指令:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.list = ['Item 1', 'Thing 2', 'Stuff part 3'];
  $scope.messages = ['No events yet'];
  $scope.name = 'World';

  $scope.myTapHandler = function(item) {
    $scope.messages.unshift('Tap: ' + item);
  }

});

app.directive('onTap', function($timeout) {
  return {
    restrict: 'A',
    link: function($scope, $elm, $attrs) {
      $elm.bind('touchend', function(evt) {
        // Prevent the onLongPress event from firing
        $scope.longPress = false;
        // If there is an on-touch-end function attached to this element, apply it
        if ($attrs.onTap) {
          $scope.$apply(function() {
            $scope.$eval($attrs.onTap)
          });
        }
      });
    }
  };
});

以下是重要的HTML位:

<body ng-controller="MainCtrl">
  <h3>Things to long-press</h3>
  <p ng-repeat="item in list" on-tap="myTapHandler(item)">
    [{{ item }}]
  </p>

  <h3>Messages (instead of console)</h3>
  <p ng-repeat="msg in messages">{{msg}}</p>
</body>