我正在做复杂的触摸手势,遇到这个问题:
let cachedStartTouches: TouchList;
let cachedMoveTouches: TouchList;
function onStart(ev: TouchEvent) {
// length equals 2 when two fingers pinch start
cachedStartTouches = ev.touches;
}
function onMove(ev: TouchEvent) {
// length equals 2 when two fingers pinching moving
cachedMoveTouches = ev.touches;
}
function onEnd(ev: TouchEvent) {
// equals 1 when one finger holds up early, but another one still moving
ev.touches.length
// the cachedTouches's length is still two now,
// how to known which idx had been canceled, 0 or 1 ?
const idx = getCanceledTouchesIdx(????)
// remove it from cache
cachedStartTouches.splice(idx, 1)
cachedMoveTouches.splice(idx, 1)
}
更一般的情况:N个手指捏住,并抬起其中一些?
触摸事件有:
event.touches
包含当前活动触摸的列表event.changedTouches
包含更改的触摸列表对于
touch end
事件,这意味着可以从 event.changedTouches
列表中读取已结束的触摸,而剩余的触摸将在 event.touches
中。
或者,您可以在
event.touches
/ start
事件处理程序中跟踪 move
,并查看哪些触摸从 event.touches
事件处理程序的 end
列表中消失。