Graphics Reference
In-Depth Information
LISTING 13-9 TouchableView -initWithFrame: Implementation
- ( id )initWithFrame:(CGRect)frame
{
if (!( self = [ super initWithFrame :frame])) return nil ;
colorArray = [[NSArray alloc] initWithObjects:[UIColor greenColor],
[UIColor blueColor], [UIColor redColor], nil ];
colorIndex = 0;
[ self setBackgroundColor:[ colorArray objectAtIndex: colorIndex ]];
touchBeganPoint = CGPointZero;
[ self setUserInteractionEnabled: YES ];
return self ;
}
After calling the super 's initWithFrame , build the color array for the view to rotate
through and set the initial colorIndex to zero. Next, set the view's backgroundColor
based on that index, and then set the userInteractionEnabled to YES so that the applica-
tion can receive single touch events.
With touch events enabled, we need to implement all the touch methods to receive those
events. The first one is -touchesBegan: withEvent: , as shown in Listing 13-10.
LISTING 13-10 -touchesBegain:withEvent: Implementation
- ( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *beginTouch = [touches anyObject];
touchBeganPoint = [beginTouch locationInView:[ self window]];
lastUpdatedPoint = [beginTouch locationInView:[ self window]];
}
This method records the location of the beginning touch with touchBeganPoint and sets
the lastUpdatedPoint to the same location. We grab any of the touches from the set
because we are configured only for single touch events, so it is irrelevant as to which
touch we get.
Now that the touch workflow is set, we need to wait for the next event before we can
perform any action. One of three possible methods will be called, the easiest of which to
implement is the -touchesCancelled:withEvent: , as shown in Listing 13-11.
 
Search WWH ::




Custom Search