标签:
The touchcancel
event is often neglected when building touch–interfaces using JavaScript. Historically, browsers vendors have never really published documentation detailing the circumstances as to when this event gets fired, and hence it has always been associated with a level of obscurity by many developers. The aim of this post is to try and shed some light on the matter.
Apple originally created touch events for iOS Safari, which have since been adopted as a basis for the W3C Touch Events Specification. The originalSafari DOM Additions Reference documentation provides very little information on touchcancel:
Sent when the system cancels tracking for the touch.
Thankfully, the W3C Touch Events specification gives us a bit more detail, including some situations where the event should occur:
A user agent must dispatch this event type to indicate when a touch point has been disrupted in an implementation-specific manner, such as a synchronous event or action originating from the UA canceling the touch, or the touch point leaving the document window into a non-document area which is capable of handling user interactions. (e.g. The UA’s native user interface, plug-ins) A user agent may also dispatch this event type when the user places more touch points on the touch surface than the device or implementation is configured to store, in which case the earliest Touch object in the TouchList should be removed.
From this description we can begin to understand touchcancel with regard to browser behaviour a little better. It not only helps the browser keep accurate references to active Touch
objects in the TouchList
, but it can also be used by developers to carry out UI specific tasks, such as reseting any variables used during touchstart
or touchmove
. In the event that the system cancels tracking and the touchend
event does not fire, touchcancel saves the day.
But when do browsers actually trigger this event? To start investigating, it was necessary to build a simple event logger to track all touch events. The test features a touch-area and logs the last touch event fired, making it easier to see exactly what kind of interactions result in a touchcancel.
What follows is a list of findings for various mobile web browsers. It is interesting to note that touchcancel is not very predictable, especially on Android (2.3.5). But even on iOS5 you cannot always correctly guess what will trigger a touchcancel.
Actions that do trigger touchcancel during a touchstart or touchmove event:
Actions that do not trigger touchcancel during a touchstart or touchmove event:
In some of these circumstances the browser will simply fire a touchend instead of touchcancel. In others situations (such as the old–style popup notifications), touchstart or touchmove will remain active (even while the content is hidden underneath the notification), until the user releases their finger, at which point touchend is fired.
Notable iOS5 system functions that will not activate during a touchstart or touchmove event:
Actions that do trigger touchcancel during a touchstart or touchmove event:
Actions that do not trigger touchcancel during a touchstart or touchmove event:
Notable Android 2.3.5 system functions that will not activate during a touchstart or touchmove event:
Opera Mobile 11.50 for Android was also tested. While the browser appears to have support for touchcancel, it does not actually seem to fire the event. In the case of pressing the home button for example, Opera Mobile appears to immediately fire touchend instead.
Even though the browser reportedly supports the touchcancel event, it does not appear to get fired at all. Even when pressing the power button to put the device on standby, touchcancel is not fired. The same goes for pressing browser tool bar buttons, or focusing on the address bar. It is also worth noting that when performing a touchstart or touchmove, the Playbook does not respond to any bevel swipe gestures.
It is appears from the tests that touchcancel is still a bit unpredictable. While it is easy enough to identify situations where it gets fired, there are often near identical situations where it does not. While the usefulness of this event is by no means in question, it seems like developers cannot yet rely on this event getting fired in all expected circumstances.
What is clear though, is that you cannot rely on touchend getting fired on every device. You need to write code that deals with touchcancel. This is especially true on the iPad for example, say if you’re building a multi–touch app or multi–player HTML5 game. Since there is no way of telling if the user has enabled Multitasking Gestures, your app needs to handle touchcancel gracefully. The unpredictable nature of Android browser also demonstrates the importance of dealing with this event, as failure to do so will likely cause bugs in your UI.
So, next time you’re writing your own touch events, don’t forget about touchcancel. Despite the varying levels of browser support, it still comes in very handy when dealing with certain unexpected user interactions.
Different ways to trigger touchcancel in mobile browsers
标签:
原文地址:http://www.cnblogs.com/aloha/p/4831273.html