Graphics Programs Reference
In-Depth Information
For the More Curious: UIControl
The class
UIControl
is the superclass for several classes in Cocoa Touch, including
UIButton
and
UISlider
. We've seen how to set the targets and actions for these con-
trols. Now we can take a closer look at how
UIControl
overrides the same
UIRespon-
der
methods you implemented in this chapter.
In
UIControl
, each possible
control event
is associated with a constant. Buttons, for ex-
ample, typically send action messages on the
UIControlEventTouchUpInside
con-
trol event. A target registered for this control event will only receive its action message if
the user touches the control and then lifts the finger off the screen inside the frame of the
control. Essentially, it is a tap.
For a button, however, you can have actions on other event types. For example, you might
trigger a method if the user removes the finger
inside or outside
the frame. Assigning the
target and action programmatically would look like this:
[rButton addTarget:tempController
action:@selector(resetTemperature:)
forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
Now consider how
UIControl
handles
UIControlEventTouchUpInside
.
// Not the exact code. There is a bit more going on!
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Reference to the touch that is ending
UITouch *touch = [touches anyObject];
// Location of that point in this control's coordinate system
CGPoint touchLocation = [touch locationInView:self];
// Is that point still in my viewing bounds?
if (CGRectContainsPoint([self bounds], touchLocation))
{
// Send out action messages to all targets registered for this event!
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
} else {
// The touch ended outside the bounds, different control event
[self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}
}
So how do these actions get sent to the right target? At the end of the
UIResponder
method implementations, the control sends the message
sendActionsForCon-