3

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.)

Grumdrig
  • 16,588
  • 14
  • 58
  • 69

4 Answers4

4

You must add this to your controller:

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

-(BOOL)becomeFirstResponder
{
    return YES;
}
exeapps
  • 119
  • 1
  • 4
3

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];
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • a more indepth answer would be on http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone by Joe D'Andrea – LolaRun Jun 08 '11 at 07:45
  • Did you notice you verified `event.type` against `UIEventSubtypeMotionShake` which is a subtype? By coincidence this worked because `UIEventSubtypeMotionShake` and `UIEventTypeMotion` have the same value. – bio Dec 14 '15 at 15:02
0

You can't become the first responder before your view is in the view hierarchy.

tc.
  • 33,468
  • 5
  • 78
  • 96
  • I tried calling it other places too but it didn't seem to help - like in `drawView`. – Grumdrig Aug 21 '10 at 20:28
  • Can you create a view controller to stick the EAGLView in, and call becomeFirstResponder in viewDidAppear:? – tc. Aug 23 '10 at 01:56
  • I was sorta trying to avoid that if I can. `UIView`s are `UIResponder`s, if I'm not mistaken. – Grumdrig Aug 24 '10 at 18:09
0

Updating to the latest version of the SDK magically fixed the problem. [Shrug]

Grumdrig
  • 16,588
  • 14
  • 58
  • 69