【问题标题】:Handling touch start and touch end events处理触摸开始和触摸结束事件
【发布时间】:2012-10-26 15:43:33
【问题描述】:

我正在尝试为触摸设备制作基于画布的益智游戏,但我正在为触摸事件而苦苦挣扎。我希望 touchFunction 触发两次,一次用于 touchstart 事件,一次用于具有适当 x 和 y 坐标的 touchend 事件。

以下是我所拥有的,它适用于 touchstart,但对于 touchend 无效,谁能指出我的问题是什么?任何帮助都会很棒,谢谢。

    canvas.addEventListener('touchstart', touchFunction, false);
    canvas.addEventListener('touchend', touchFunction, false);


    function touchFunction(ev) {   
    console.log(ev.touches[0].pageX + " " + ev.touches[0].pageY);
    }

【问题讨论】:

    标签: javascript touch


    【解决方案1】:

    你看过touchmove事件吗?

    这将为您提供当前 'press' 所在位置的 x-y 坐标。非常类似于mousemove

    【讨论】:

    • 是的,但我只想要触摸的最后一个位置,那么我将如何将最后一个 touchmove 位置与 touchend 事件结合起来?
    • 跟踪来自touchmove 的最后坐标。通常当我在编写这些类型的处理程序时,start 事件只是表示我想开始收听moveend 表示我需要最终确定最后捕获的位置。
    • 好的,谢谢,所以我有一个函数将 touchmove 上的 x 和 y 位置设置为 touchend 事件可以使用的变量。感谢您的帮助。
    【解决方案2】:

    您可能希望研究像hammer.js 这样的抽象层,这样您就可以处理鼠标事件以及各种特定于平台的触摸事件:http://eightmedia.github.com/hammer.js/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2015-04-19
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多