Tracking Touches
While Skippy (the inspiration for the RoadTrip app) was pretending to drive across country, he said that it would be nice to be able to drag the car and place it anywhere on the screen. And because his wish is my command, in this section I explain how to code for dragging an object, as well as how touches work on the iPad.
The touch of a finger (or lifting it from the screen) adds a touch event to the application’s event queue, where it’s encapsulated (contained) in a UIEvent
object. A UITouch
object exists for each finger touching the screen, which enables you to track individual touches.
The touchesBegan:withEvent:
message is sent when one or more fingers touch down in a view. This message is a method of the TestDrive Controller’s
superclass, UIResponder
, from which the view controller is derived. (I explain this in Chapter 6, in the section about UIApplicationMain
.)
As the user manipulates the screen with his or her fingers, the system reports the changes for each finger in the corresponding UITouch
object, thereby sending the touchesMoved:withEvent:
message. The touches Ended:withEvent:
message is sent when one or more fingers lift from the associated view. The touchesCancelled:withEvent:
message, on the other hand, is sent when a system event (such as a low-memory warning) cancels a touch event.
In this app, you need be concerned only with the first two methods just described.
To begin the process of responding to a touch event, add a new instance variable ...
Get iPad Application Development For Dummies, 3rd Edition 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.