触摸事件未返回触摸数据

12

编辑:如果确实无法在Angular UI模态框中执行此操作,“这不能在Angular UI模态框中完成”是一个有效的回答。

以下是我从触摸事件返回的数据。明显缺少任何有用的触摸X/Y坐标(https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches)。这是一个非常普遍的问题,但还是有想法吗?传递给在触摸上执行的函数的“event”对象:

{
      "originalEvent": {
        "isTrusted": true
      },
      "type": "touchstart",
      "timeStamp": 1450388006795,
      "jQuery203026962137850932777": true,
      "which": 0,
      "view": "$WINDOW",
      "target": {},
      "shiftKey": false,
      "metaKey": false,
      "eventPhase": 3,
      "currentTarget": {},
      "ctrlKey": false,
      "cancelable": true,
      "bubbles": true,
      "altKey": false,
      "delegateTarget": {},
      "handleObj": {
        "type": "touchstart",
        "origType": "touchstart",
        "data": null,
        "guid": 2026,
        "namespace": ""
      },
      "data": null
    }

现在,这是在Angular UI模态框中的画布中,但鼠标事件正常工作。顺便说一下,这是我的元素:

link: function(scope, element, attrs, model){
                //scope.canvasElem = element[0].children[0].children[0];
                scope.canvasElem = angular.element($('.touchScreen'))[0];
                scope.ctx = scope.canvasElem.getContext('2d');

以下是我如何绑定的示例:

element.bind('touchstart', scope.touchStart);

编辑,这里有一个mousedown事件对象供比较:

{
  "originalEvent": {
    "isTrusted": true
  },
  "type": "mousedown",
  "timeStamp": 1450389131400,
  "jQuery20309114612976554781": true,
  "toElement": {},
  "screenY": 436,
  "screenX": 726,
  "pageY": 375,
  "pageX": 726,
  "offsetY": 81,
  "offsetX": 41,
  "clientY": 375,
  "clientX": 726,
  "buttons": 1,
  "button": 0,
  "which": 1,
  "view": "$WINDOW",
  "target": {},
  "shiftKey": false,
  "relatedTarget": null,
  "metaKey": false,
  "eventPhase": 3,
  "currentTarget": {},
  "ctrlKey": false,
  "cancelable": true,
  "bubbles": true,
  "altKey": false,
  "delegateTarget": {},
  "handleObj": {
    "type": "mousedown",
    "origType": "mousedown",
    "data": null,
    "guid": 2025,
    "namespace": ""
  },
  "data": null
}
3个回答

2
例如,您可以使用指令绑定触摸事件,在指令内部可以有以下代码:
element.on('touchstart', function (event) {
     event = event.originalEvent || event;
     //actions
     event.stopPropagation();   //if you need stop the propagation of the event
});

如果涉及到it技术,你需要注意四种触摸事件(touchstart、touchend、touchmove、touchcancel)的行为,根据你的目标进行谨慎处理。

如果要捕获光标位置,请使用以下代码:

element.on('touchmove', function (event) {
    event.preventDefault();
    if (event.targetTouches.length == 1) {  //when only has one target in touch
         var touch = event.targetTouches[0];
        // Place element where the finger is
        var x = touch.pageX + 'px';
        var y = touch.pageY + 'px';
    }
    event.stopPropagation();
});

1
var touchRelativeToViewWindow = {
    x: event.originalEvent.touches[0].pageX,
    y: event.originalEvent.touches[0].pageY
};

迟做总比不做好(注意,这是针对touchmove):

在使用AngularJs/jQuery时,我们需要查看originalEvent中的所有细节。请注意,[0]部分可能表示多个同时触摸。我不知道它们的顺序,但touches[0]可能是第一只手指,touches[1]可能是第二只手指。上面的解决方案对我有用,因为我只希望用户使用笔。


1
这是正确的答案。这里有两个问题: 1)在使用jQuery时,您必须使用originalEvent(如VSO在此处所述) 2)当检查事件对象时,您无法轻松地将其转储为JSON以查看所有内容-事件对象包括循环的DOM对象引用。因此,尽管它不会显示在JSON转储中,但确实存在一个event.originalEvent.touches []数组。请参见此处的讨论:https://dev59.com/4Wgu5IYBdhLWcg3wJDyb - David Pritchard

0
尝试使用以下代码获取x和y坐标:
x1 = event.touches[0].pageX;
y1 = event.touches[0].pageY;

事件已发布。它们没有touches属性。 - VSO

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接