Different ways to trigger touchcancel in mobile browsers

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.

iOS Safari 5.0.1 (iPhone 4 & iPad 1)

Actions that do trigger touchcancel during a touchstart or touchmove event:

  • User presses the home button.
  • User presses the action button in Safari’s bottom toolbar.
  • User presses/smears the palm of their hand on the screen, creating no discernible single touch points (iPhone only?).
  • User presses the lock button.
  • User initiates a gesture using four or more simultaneous touch points on iPad and Multitasking Gestures are enabled in system preferences.

Actions that do not trigger touchcancel during a touchstart or touchmove event:

  • User presses the bookmark button in Safari’s bottom toolbar.
  • User presses the back or forward history buttons in Safari’s bottom toolbar.
  • User taps to focus on the Safari search bar.
  • User rotate’s the device orientation.
  • Popup notification such as WiFi locator or Clock timer alert appears on screen.
  • New iOS5 style notification appears at top of the screen.
  • User opens a new tab (iPad).
  • User refreshes the page.
  • User presses the volume buttons.
  • User switches the device onto silent/vibrate.
  • User receives a phone call.

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:

  • User cannot focus on the Safari address bar.
  • User cannot open a new page (iPhone).
  • User cannot double–tap the home button to open the app tray.
  • User cannot swipe down from the top of screen to open notification centre.
  • User cannot tap on a new iOS5 style notification when it appears at the top of the screen.

Android 2.3.5 (Samsung Galaxy Y GT-S5363) stock browser

Actions that do trigger touchcancel during a touchstart or touchmove event:

  • User presses the lock button.
  • Appears to fire randomly for no immediately obvious reason. This is especially noticeable when performing repeated quick taps and swipes.

Actions that do not trigger touchcancel during a touchstart or touchmove event:

  • User presses the home button.
  • User rotate’s the device orientation.
  • User presses the volume buttons.

Notable Android 2.3.5 system functions that will not activate during a touchstart or touchmove event:

  • User cannot press the menu or back buttons.
  • User cannot open notifications menu.
  • User cannot focus on the browser address bar.
  • User cannot tap the bookmarks button.

Opera Mobile

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.

BlackBerry Playbook (1.0.8.6067)

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.

Conclusion

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.

时间: 2024-08-26 04:20:38

Different ways to trigger touchcancel in mobile browsers的相关文章

Top 6 Programming Languages for Mobile App Development

Mobile application development industry in the last five years have multiplied in leaps and bounds, changing the way businesses function worldwide. With enterprises aligning mobile apps to their productivity in the recent times, and with the rapid in

HTML5 tricks for mobile

iOS 7.1的Safari为meta标签新增minimal-ui属性,在网页加载时隐藏地址栏与导航栏 01. Creating a fullscreen experience On Android Browser – the default browser on Android up to 4.3, and different to Chrome – the only solution is to have a document with a height at least equal to

jQuery Mobile panel的相关属性

参考网站:http://www.lampweb.org/jquerymobile/19/64.html 面板 data-role="panel" 在一个 jQuery Mobile “page”中,面板与“header”.“footer”.“content”,必须是兄弟元素.可以在它们之前或之后添加面板的标记,但不在它们中间添加.注:此约束将在未来的版本中删除. <div data-role="page"> <div data-role="

Jquery mobile 新手问题总汇

http://www.wglong.com/main/artical!details?id=4#q9 2013-04-22 / 分类:JqueryMobile / 标签:JqueryMobile,Jqm,jquerymobile教程,jqm教程 此文章将会持续更新,主要收录一些新手比较常见的问题. 欢迎 向我推荐比较典型的常见问题,我会记录并整理进文章,方便自己更方便大家. 文章导读: 1.页面缩放显示问题 2.页面跳转后样式丢失js失效 3.跳转时重复调用pageinit方法的解决办法 4.如

Mobile Web开发实践

移动设备的快速发展给用户带来了很大的便利.用户使用Android.iPhone和其它移动设备很容易接入互联网.移动设备对网页的性能要求比较高,下面就说说Mobile Web开发的一些心得. Viewport 当你用iPhone访问一个没有面向移动设备优化过的网站时(最好选取960px宽度的网站),你会发现iPhone上面刚好可以把整个页面显示出来,但是页面被缩小了非常多,字体非常小.这其实是Sarari默认把自己当成980px宽度来处理的,而所有iPhone(竖屏)的真实宽度为320px,事实上

关于伪类元素:before和:after

关于伪类元素:before和:after Posted@2011-11-02 3:02 p.m. Categoriescss :before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: #example:before { content: "#"; color: red; } #example:after { content: "$"; color: red; } 这段代码

What technical details should a programmer of a web application consider before making the site public?

What things should a programmer implementing the technical details of a web application consider before making the site public? If Jeff Atwood can forget about HttpOnly cookies, sitemaps, and cross-site request forgeries all in the same site, what im

5 essential skills every Web Developer should have?

The idea here is that most of us should already know most of what is on this list. But there just might be one or two items you haven't really looked into before, don't fully understand, or maybe never even heard of. Interface and User Experience Be

Understanding Responsive Web Design: Cross-browser Compatibility

http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the last article, I described three ways to work with with images from a responsive design standpoint, discussing the best solution to adopt while serving pr