Graphics Reference
In-Depth Information
Each of these events is rather self-explanatory, but there are a few points worth
mentioning.
Because Cocoa Touch is designed as a multitouch system, it is possible to receive more
than one
UITouch
event in any of the
touches
XXX
methods. However, this occurs
only if we specifically enable multitouch in our
UIView
. Multitouch is enabled via the
-setMultipleTouchEnabled:
method on the
UIView
and by passing the Boolean value of
YES
. Even with multitouch enabled, however, only touches that actually occur within the
view's bounds will be passed to that view. Therefore, if the view is small enough, it is
quite possible that an intended multitouch event will result in a single
UITouch
event
being passed to the
UIView
.
Touch events occur in a specific order and never deviate from it. All touch event work-
flows start with a
touchesBegan
event and always end with either a
touchesEnded
or a
touchesCancelled
event. A single event flow (beginning with
touchesBegan
) never has
both a
touchesCancelled
and a
touchesEnded
. It is possible to receive zero or more
touchesMoved
events between
touchesBegan
and the end of the flow. There is no limit to
the number of
touchesMoved
events that a particular flow can receive; they are not timed
events. As such, if a user touches the screen, moves her finger, pauses, and then moves it
again, no events will be sent to the receiver during the pause period.
Finally, touch events—no matter how many actual touch points are involved—will be
sent all at once. In other words, if we start with a single finger touching the screen, we get
one
touchesBegan
. If a second finger is added, another
touchesBegan
event is sent for the
second finger, but a
UITouch
event is included in that call for the first finger. If one or
both fingers are moved, a single
touchesMoved
event is sent for both fingers. Each of the
touches
XXX
methods pass in
NSSet
to the receiving class. This
NSSet
contains one or
more
UITouch
objects that describe the current state of that touch event. An
NSSet
is used
as the order of these events has no value, and you should not assume that a
UITouch
event set will be passed in any specific order.
Setting Up the Xcode Project
In this example, we present users with a single box on the screen represented by a
UIView
object. When the users tap on the box, it rotates through a set number of colors. If they
touch and drag the box, it follows their finger around on the screen. This demonstrates
how to determine the difference between a tap and a drag, and how to animate the reac-
tion to a user touching the screen. We also make it so the box transforms slightly in
response to the action when the user taps on the box.
For this project, we begin with a
window-based template, as shown in
Figure 13-6. This template provides us
with just a window without any type of
navigational aid. Within this project we
create two additional classes:
TouchMeViewController
and
NOTE
We recommend running the finished project
in the iPhone Simulator to see how it works
and reacts before following along with how to
build the application.