Created
September 10, 2010 07:29
-
-
Save joseph/573248 to your computer and use it in GitHub Desktop.
Making sense of touch events on iframes in iOS 4.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function translateTouchEvent(element, target, evt, edata) { | |
// IN 4.1 ONLY, if we have a touch start on the layer, and it's been | |
// almost no time since we had a touch end on the layer, discard the start. | |
// (This is the most broken thing about 4.1.) | |
if ( | |
p.brokenModel_4_1 && | |
!edata.frame && | |
!p.touching && | |
edata.start && | |
p.edataPrev && | |
p.edataPrev.end && | |
(edata.time - p.edataPrev.time) < 30 | |
) { | |
evt.preventDefault(); | |
return; | |
} | |
// If we don't have a touch and we see a start or a move on anything, start | |
// a touch. | |
if (!p.touching && !edata.end) { | |
return fireStart(evt, target, edata); | |
} | |
// If this is a move event and we already have a touch, continue the move. | |
if (edata.move && p.touching) { | |
return fireMove(evt, edata); | |
} | |
if (p.brokenModel_4_1) { | |
// IN 4.1 ONLY, if we have a touch in progress, and we see a start, end | |
// or cancel event (moves are covered in previous rule), and the event | |
// is not on the iframe, end the touch. | |
// (This is because 4.1 bizarrely sends a random event to the layer | |
// above the iframe, rather than an end event to the iframe itself.) | |
if (p.touching && !edata.frame) { | |
// However, a touch start will fire on the layer when moving out of | |
// the overlap with the frame. This would trigger the end of the touch. | |
// And the immediately subsequent move starts a new touch. | |
// | |
// To get around this, we only provisionally end the touch - if we get | |
// a touchmove momentarily, we'll cancel this touchend. | |
return fireProvisionalEnd(evt, edata); | |
} | |
} else { | |
// In older versions of MobileSafari, if the touch ends when we're | |
// touching something, just fire it. | |
if (edata.end && p.touching) { | |
return fireProvisionalEnd(evt, edata); | |
} | |
} | |
// IN 4.1 ONLY, a touch that has started outside an iframe should not be | |
// endable by the iframe. | |
if ( | |
p.brokenModel_4_1 && | |
p.originator != element && | |
edata.frame && | |
edata.end | |
) { | |
evt.preventDefault(); | |
return; | |
} | |
// If we see a touch end on the frame, end the touch. | |
if (edata.frame && edata.end && p.touching) { | |
return fireProvisionalEnd(evt, edata); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment