blaster
blaster

Reputation: 8937

Ionic - Touch Event for When the Touch Began on a Different Element?

I am using Ionic to make a word game where the user can select a series of adjacent letters in a single swipe. The letters are implemented as individual div elements.

I know what event to use for starting letter (touchstart seems to work well), but I can't figure out what event to use for the middle tiles. Touchstart doesn't work; touchdrag doesn't seem to work. In essence, the "mouse" is already down when they begin swiping over that letter in the middle.

Any insights on how I can detect ANY touch (whether it's a start or whether the user is already in the middle of a "swipe" that began on another tile)?

Upvotes: 0

Views: 965

Answers (1)

area28
area28

Reputation: 1425

You can use elementFromPoint with a touchmove event. So inside your touchmove callback function, include the (e) parameter for the event and try this to gather all elements touched.

var touchedIds = [];
var touches = e.originalEvent.touches;

for(var x = 0; x < touches.length; x++) {
    var touch = e.originalEvent.touches[x];
    var element = document.elementFromPoint(touch.clientX, touch.clientY);

    touchedIds[x] = element; // however you want to add it to the array. 
}

You end up with an array of elements touched.You may need to filter out the extras to get a distinct array of them, but that would be trivial.

Upvotes: 1

Related Questions