I have a custom class (MyEventHandler) which has nothing to do with the UI in my app. I would like it to be able to catch a MotionShake event. As I understand I need to make MyEventHandler a first responder for that.
Here's what I've done.
I inherited MyEventHandler
from UIResponder
and implemented canBecomeFirstResponder
method. However when I call becomeFirstResponder
it returns 0, so does the [self isFirstResponder]
.
I checked the current first responder and it turned out to be nil
.
If I do the same things with my ViewController it works fine, so my question is: is it even possible to make a custom class, which doesn't inherit from UIView, a first responder?
Here's the code that I have:
@interface MyEventHandler : UIResponder
- (void)start;
@end
@implementation MyEventHandler
- (void)start {
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"Shake-shake-shake!");
}
}
@end