About Events in iOS
Users manipulate their iOS devices in a number of ways, such as touching the screen or shaking the device. iOS interprets when and how a user is manipulating the hardware and passes this information to your app. The more your app responds to actions in natural and intuitive ways, the more compelling the experience is for the user.
At a Glance
Events are objects sent to an app to inform it of user actions. In iOS, events can take many forms: Multi-Touch events, motion events, and events for controlling multimedia. This last type of event is known as a remote control event because it can originate from an external accessory.
UIKit Makes It Easy for Your App to Detect Gestures
iOS apps recognize combinations of touches and respond to them in ways that are intuitive to users, such as zooming in on content in response to a pinching gesture and scrolling through content in response to a flicking gesture. In fact, some gestures are so common that they are built in to UIKit. For example,
UIControl subclasses, such as
UISlider, respond to specific gestures—a tap for a button and a drag for a slider. When you configure these controls, they send an action message to a target object when that touch occurs. You can also employ the target-action mechanism on views by using gesture recognizers. When you attach a gesture recognizer to a view, the entire view acts like a control—responding to whatever gesture you specify.
Gesture recognizers provide a higher-level abstraction for complex event handling logic. Gesture recognizers are the preferred way to implement touch-event handling in your app because gesture recognizers are powerful, reusable, and adaptable. You can use one of the built-in gesture recognizers and customize its behavior. Or you can create your own gesture recognizer to recognize a new gesture.
An Event Travels Along a Specific Path Looking for an Object to Handle It
When iOS recognizes an event, it passes the event to the initial object that seems most relevant for handling that event, such as the view where a touch occurred. If the initial object cannot handle the event, iOS continues to pass the event to objects with greater scope until it finds an object with enough context to handle the event. This sequence of objects is known as a responder chain, and as iOS passes events along the chain, it also transfers the responsibility of responding to the event. This design pattern makes event handling cooperative and dynamic.
A UIEvent Encapsulates a Touch, Shake-Motion, or Remote-Control Event
Many events are instances of the UIKit
UIEvent class. A
UIEvent object contains information about the event that your app uses to decide how to respond to the event. As a user action occurs—for example, as fingers touch the screen and move across its surface—iOS continually sends event objects to an app for handling. Each event object has a type—touch, “shaking” motion, or remote control—and a subtype.
An App Receives Multitouch Events When Users Touch Its Views
Depending on your app, UIKit controls and gesture recognizers might be sufficient for all of your app’s touch event handling. Even if your app has custom views, you can use gesture recognizers. As a rule of thumb, you write your own custom touch-event handling when your app’s response to touch is tightly coupled with the view itself, such as drawing under a touch. In these cases, you are responsible for the low-level event handling. You implement the touch methods, and within these methods, you analyze raw touch events and respond appropriately.
An App Receives Motion Events When Users Move Their Devices
Motion events provide information about the device’s location, orientation, and movement. By reacting to motion events, you can add subtle, yet powerful features to your app. Accelerometer and gyroscope data allow you to detect tilting, rotating, and shaking.
Motion events come in different forms, and you can handle them using different frameworks. When users shake the device, UIKit delivers a
UIEvent object to an app. If you want your app to receive high-rate, continuous accelerometer and gyroscope data, use the Core Motion framework.
An App Receives Remote Control Events When Users Manipulate Multimedia Controls
iOS controls and external accessories send remote control events to an app. These events allow users to control audio and video, such as adjusting the volume through a headset. Handle multimedia remote control events to make your app responsive to these types of commands.
This document assumes that you are familiar with:
The basic concepts of iOS app development
The different aspects of creating your app’s user interface
How views and view controllers work, and how to customize them
If you are not familiar with those concepts, start by reading Start Developing iOS Apps Today. Then, be sure to read either View Programming Guide for iOS or View Controller Programming Guide for iOS, or both.
In the same way that iOS devices provide touch and device motion data, most iOS devices have GPS and compass hardware that generates low-level data that your app might be interested in. Location and Maps Programming Guide discusses how to receive and handle location data.
For advanced gesture recognizer techniques such as curve smoothing and applying a low-pass filter, see WWDC 2012: Building Advanced Gesture Recognizers.
Many sample code projects in the iOS Reference Library have code that uses gesture recognizers and handles events. Among these are the following projects:
Simple Gesture Recognizers is a perfect starting point for understanding gesture recognition. This app demonstrates how to recognize tap, swipe, and rotate gestures. The app responds to each gesture by displaying and animating an image at the touch location.
Handling Touches Using Responder Methods and Gesture Recognizers includes two projects that demonstrate how to handle multiple touches to drag squares around onscreen. One version uses gesture recognizers, and the other uses custom touch-event handling methods. The latter version is especially useful for understanding touch phases because it displays the current touch phase onscreen as the touches occur.
MoveMe shows how to animate a view in response to touch events. Examine this sample project to further your understanding of custom touch-event handling.