Question

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

Was it helpful?

Solution 4

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

OTHER TIPS

You must add this to your controller:

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

-(BOOL)becomeFirstResponder
{
    return YES;
}

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];

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top