Graphics Programs Reference
In-Depth Information
Turning Touches Into Lines
A line (remember 9th grade geometry class?) is defined by two points. Our
Line
stores
these points as properties named
begin
and
end
. When a touch begins, you'll create a
line and set both
begin
and
end
to the point where the touch began. When the touch
moves, you will update
end
. When the touch ends, you will have your complete line.
There are two collection objects that hold
Line
instances. Lines that have been completed
are stored in the
completeLines
array. Lines that are still being drawn, however, are
stored in an
NSMutableDictionary
. Why do we need a dictionary? We've enabled
multi-touch, so a user can draw more than one line at a time. This means we have to keep
track of which touch events go with which line. For instance, imagine the user touches the
screen with two fingers creating two instances of
Line
. Then one of those fingers moves.
The
TouchDrawView
is sent a message for the event, but how can it know which line to
update?
This is why we're using a dictionary instead of an array for the lines in process. When a
touch begins, we will grab the address of the
UITouch
object that is passed in and wrap it
in an
NSValue
instance. A new
Line
will be created and added to the dictionary, and the
NSValue
will be its key. As we receive more touch events, we can use the address of the
UITouch
that is passed in to access and update the right line (
Figure 19.2
).
Figure 19.2 Object diagram for TouchTracker