Su touchstart
è possibile memorizzare tutti i valori necessari (come x, y, destinazione, ecc.). Su touchend
puoi recuperare tutti i valori memorizzati grazie al valore Touch.identifier
che dovrebbe essere unico per ogni tocco.
ho creato un proof of concept qui: http://jsbin.com/adifit/3/
Il codice qui sotto registra posizione x
e y
solo, ma si può tenere traccia di una qualsiasi delle proprietà, se avete bisogno.
L'idea alla base del codice è:
- su
touchstart
creare un oggetto e memorizzare tutti i dati all'interno (compreso il tocco ID)
- negozio di questo oggetto nella matrice
- sul
touchend
controllo della id del tocco e prova a trovare l'oggetto corrispondente nell'array
- se trovato di quanto abbiamo fatto.
E il codice:
var touches = [];
var cons;
$(init);
function init()
{
cons = $("#console");
document.getElementById("area").addEventListener("touchstart", onTouchStart);
document.addEventListener("touchend", onTouchEnd);
document.addEventListener("touchcancel", onTouchEnd);
}
function onTouchStart(e)
{
e.preventDefault();
var touchList = e.changedTouches;
var touch;
for(var i = 0; i < touchList.length; i++)
{
cons.html(cons.html() + "startX: " + touchList[i].screenX + ", id: " + touchList[i].identifier + "<br/>");
touch = {x: touchList[i].screenX, y: touchList[i].screenY, id: touchList[i].identifier};
touches.push(touch);
}
}
function onTouchEnd(e)
{
cons.html(cons.html() + "<strong>TouchEnd:</strong><br/>");
var touchList = e.changedTouches;
var touch;
for(var i = 0; i < touchList.length; i++)
{
touch = {x: touchList[i].screenX, y: touchList[i].screenY, id: touchList[i].identifier};
for (var j = touches.length - 1; j >= 0 ; j--)
{
if (touches[j].id == touch.id)
{
cons.html(cons.html() + "<strong>startX: "+ touches[j].x+ ", id: " + touchList[i].identifier + "</strong><br/>");
touches.splice(j, 1);
}
}
}
}
Il codice precedente utilizza jQuery, ma viene utilizzato solo per comodità di visualizzazione dei risultati sullo schermo, jQuery non viene utilizzato per altri scopi.
Perché non si memorizza la x, y quando il tocco si avvia e si ripresenta dopo la release ... –
Ho aggiornato la mia risposta, si spera che sia quello che stavi cercando. – strah