Chapter 5. Handling Touch Interactions and Events

An essential part of any Web 2.0 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. While 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 of this activity taking place and fires off events as they occur.

With its touch screen interface, iPhone is all about direct interactivity with the user. As a result, you would expect any iPhone/iPod touch application you create to be able to handle the variety of finger taps, flicks, swipes, and pinches that a user naturally performs as they interact with their mobile device. However, because of the current capabilities of Mobile Safari browser, you have to work with these interactions differently than what you might expect.

How iPhone Handles Events

When working with touch interactions and events for iPhone, keep in mind that the finger is not a mouse. As a result, the traditional event model that Web developers are so used to working with does not always apply in this new context. This is both good news and bad news for the application developer. The good news is that much of the touch interaction that iPhone and iPod touch are famous for is built right into Mobile Safari. As a result, you do not need to write any code to handle the basic touch interactions ...

Get Professional iPhone™ and iPod® touch Programming: Building Applications for Mobile Safari™ 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.