angularjs - 如何使用 Angular 指令处理单个 HTML 元素上的多个触摸事件

标签 angularjs touch swipe

在我的移动应用程序中,我有一个以 HTML 格式显示内容的主视图:

<div class="imageView" na-swipe="next()" na-tap="showControls()"> content here </div>

为了使用 jQuery(或 Hammer.js)处理 swipetouch 事件,我创建了这两个指令:

angular.module("naModule").directive 'naTap', ->
  (scope, element, attrs) ->
    tapping = false
    element.bind 'touchstart', -> tapping = true
    element.bind 'touchmove', -> tapping = false
    element.bind 'touchend', -> scope.$apply(attrs['naTap']) if tapping

angular.module("naModule").directive 'naSwipe', ->
  (scope, element, attrs) ->
    tapping = false
    swiping = false
    element.bind 'touchstart', -> tapping = true
    element.bind 'touchmove', -> swiping = true
    element.bind 'touchend',  -> scope.$apply(attrs['naSwipe']) if (swiping and tapping)

naSwipe 似乎运行良好,即每当执行滑动时都会调用 next()...但是,当我点击 next() 时showControls() 都在触发...如何干净地分离这个 div 上的触摸和滑动? 谢谢。

最佳答案

我认为您针对您的用例以错误的方式处理此问题。

考虑使用一个指令,其模板是您所描述的标记,并定义要在该指令的 link() 函数中捕获的事件。

类似于以下指令:

angular.module('naModule').directive('imageView', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            // As required for your content ...
        },
        template: '<div class="imageView"> content here </div>',
        controller: function($scope, $attrs) {
            $scope.next = function() {
                // handle function..
            }

            $scope.showControls = function() {
                // handle function..
            }
        },
        link: function(scope, element, attrs, controller) {
            element.bind('touchstart', function(e) {
                scope.tapping = true;
                scope.swiping = true;
            }

            element.bind('touchmove', function(e) {
                scope.tapping = false;
                scope.swiping = true;
            }

            element.bind('touchend', function(e) {
                if(scope.tapping) {
                    scope.next();
                } else {
                    scope.showControls();
                }
            }
        }
    }
});

关于angularjs - 如何使用 Angular 指令处理单个 HTML 元素上的多个触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928485/

相关文章:

html - 使用 html css 进行图表的通用滑动手势

ios - 如何检测 Spritekit 节点上的滑动?

javascript - 类型错误: Cannot read property 'setMainPage' of undefined in onsen ui,

angularjs - ng-click angularjs 上的表单验证

css - 基于触摸功能而不是视口(viewport)媒体查询的样式?

iphone - [event allTouches] 和 [touches allObjects] 之间的区别?

ios - Swift - UIScrollView 中的 UISwipeGestureRecognizer

javascript - 我怎样才能列出可用的过滤器

javascript - AngularJS 触发器并监视来自另一个模块的 Controller 的服务中的对象值更改(扩展)

java - 如何在 android 中处理 "record"和 "replay"触摸事件?