Graphics Programs Reference
In-Depth Information
Motion Events
UIResponder
can become the first responder of the window and will receive events
when the device is shaken or a key is pressed on the keyboard. You will make the instance
of
HypnosisView
the first responder of
Hypnosister
's window. Shaking the device will
send a message to the
HypnosisView
, and that method will change its
circleColor
.
To give a
UIResponder
first responder status, you send it the message
be-
comeFirstResponder
. In
HypnosisterAppDelegate.m
, tell the
Hypnos-
isView
instance to become the first responder. The
becomeFirstResponder
method
returns a Boolean value indicating whether the receiving object successfully became the
first responder of the window.
[[self window] addSubview:view];
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(@"HypnosisView became the first responder");
} else {
NSLog(@"Could not become first responder");
}
self.window.backgroundColor = [UIColor whiteColor];
Build and run the application. Notice that the console tells you that the
HypnosisView
failed to become the first responder. Most
UIResponder
objects return
NO
in
be-
comeFirstResponder
. This is because most views, by default, only care about events
connected with themselves, and they (almost) always get a chance to handle these events.
For example, a tapped
UIButton
gets sent a message regardless of who the first respon-
der is. So, a responder object must explicitly state that it is willing to become the first re-
sponder. In
HypnosisView.m
, override the
UIResponder
method
canBe-
comeFirstResponder
to return
YES
.
- (BOOL)canBecomeFirstResponder
{
return YES;
}
Build and run the application again. The console will report that the
HypnosisView
is
now the first responder.