Chapter 7. Handling Touch Interactions and Events

An essential part of any Web application is the ability to respond to events triggered by the user or by a condition that occurs on the client: the clicking of a button, the pressing of a key, the scrolling of a window. Whereas the user interacts with an HTML element, the entire document, or the browser window, JavaScript serves as the watchful eye behind the scenes that monitors all this activity taking place and fires off events as they occur.

With its touch interface, iPhone is all about direct interactivity with the user. As a result, it is not surprising that any iPhone Web app you create can handle the variety of gestures — finger taps, flicks, swipes, and pinches — that a user naturally performs as they interact with your app on their mobile device.

The Three Types of Touch Events

There are three primary types of touch-related events to consider when developing an iPhone Web app. These include the following:

  • Mouse emulation events: Events in which a one- or two-finger action simulates a mouse.

  • Touch events: Events that dispatch when one or more fingers touch the screen. You can trap touch events and work with multiple finger touches that occur at different points on the screen at the same time.

  • Gesture events: Combinations of touch events that also support scaling and rotation information.

The mouse emulation events are the ones that you'll be working with for most purposes inside your Web app. As such, I'll spend most of my time ...

Get Safari® and WebKit Development for iPhone® OS 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.