Chapter 29

Touches and Gestures

iOS devices differ from traditional non-mobile devices. Interaction is received from a touch or gesture, rather from a keyboard or mouse. The touch or gesture is a UIEvent and the UIApplication class manages these events. The events that are most common on iOS devices are touch events interacting with views.

A touch sequence begins as a finger or fingers are placed on the touch screen, and ends when the last finger is removed from the touch screen.

Single-fingered events can be a tap, touch, and hold, or the drag and swipe. Multiple-fingered events, for example, can be a pinch, commonly used to zoom in or out on a photo.

This lesson looks at the two techniques of touch event handling:

  • Use of the phase methods: touchesBegin, touchesMoved, touchesCancelled, and touchesEnded.
  • Use of the UIGestureRecognizer class.

Touch Events

A touch event is a UIEvent of the type UIEventTypeTouches. The touch itself is a UITouch object that contains the following information:

  • locationInView: The touch coordinates
  • previousLocationInView: Previous coordinates
  • tapCount: Current tap count
  • timestamp: Time of the last touch
  • phase: The current touch phase

When a touch event occurs, it is placed on a queue that is distributed by the application to the window where the event was initiated. The event is then forwarded to a first responder. In most cases, the first responder is the view where the touch occurred.

If that view cannot handle the touch event, the event is then ...

Get iPhone and iPad App 24-Hour Trainer 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.