Graphics Reference
In-Depth Information
LISTING 13-7
Continued
viewController
= [[
TouchMeViewController
alloc]
init
];
[
window
addSubview:[
viewController
view]];
[
window
makeKeyAndVisible];
}
- (
void
)dealloc
{
[
viewController
release];
[
window
release];
[
super
dealloc
];
}
The only reason we hang onto the
TouchMeViewController
reference is to allow its later
release with the
dealloc
method. All the control in this example is handled directly in
the
UIView
subclass implemented in the next section.
Implementing the TouchableView
Although the
touches
XXX
methods are stateful, they do not pass us stateful information.
Therefore we need to keep track of the current state of the touches. We need to keep track
of both where the touch began at and where the last move update was. This enables us to
move the square if the move is sufficient to justify it, and also enables us to test for a tap
as opposed to an actual move. To keep all this stateful information, we need to imple-
ment the header, as shown in Listing 13-8.
LISTING 13-8
TouchableView Header
@interface
TouchableView : UIView
{
CGPoint
touchBeganPoint
;
CGPoint
lastUpdatedPoint
;
NSArray *
colorArray
;
NSInteger
colorIndex
;
}
@end
In addition to the beginning touch point and the last updated point, we also keep track
the currently displayed color and the full array of colors that the view can rotate through.
There are a number of things we need to set up in the view's subclass, the first of which is
to override the superclass's
-initWithFrame:
method, as shown in Listing 13-9.