Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

motionEnded not being called (no view controller)

I think I've done all I should to detect a shake, but motionEnded:withEvent: never gets called. (One wrinkle is that I don't have a UIViewController - my app is based on the "OpenGL ES App" template.)

I've added application.applicationSupportsShakeToEdit = YES; to my application:didFinishLaunchingWithOptions:, and

- (BOOL)canBecomeFirstResponder { return YES; }

to EAGLView.m (which does get called), and [self becomeFirstResponder]; to initWithCoder: (and have tried various other places too).

But the debugger never hits

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 

Am I missing some step? Do I have to have a controller?

(I'm using iOS 3.2 in the iPad simulator.)

like image 617
Grumdrig Avatar asked Sep 18 '25 15:09

Grumdrig


2 Answers

You must add this to your controller:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

-(BOOL)becomeFirstResponder
{
    return YES;
}
like image 145
exeapps Avatar answered Sep 20 '25 05:09

exeapps


The way the UIResponder chain works with the shake notification is obnoxious. Seems that UIWindow always gets the notification, and then sub-responders may or may not depending on whats above them in the chain. I created a UIWindow subclass, and defined it as my window class with the following:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.type == UIEventSubtypeMotionShake) 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtypeMotionShakeEnded" object:nil];
}

Then, for any views that wanted the shake notifications, I simply had them add themselves as an observer to the UIEventSubtypeMotionShakeEnded event, and they got it every time.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeNotification:)
                                             name:@"UIEventSubtypeMotionShakeEnded" object:nil];
like image 27
BadPirate Avatar answered Sep 20 '25 06:09

BadPirate