Pergunta

I'm trying to detect a shake in iOS6, seems every single example ever was written back in 2009 or so for iOS3, and nothing's working as it should. Here's my method:

- (void) motionEnded: (UIEventSubtype) motion withEvent: (UIEvent *) event {
    if (motion == UIEventSubtypeMotionShake) NSLog(@"Detected a shake");
}

I've tried what Apple suggest (making a canBecomeFirstResponder method that returns YES, and calling it), but it doesn't do anything. It's called fine, but has no impact on the shake not being recognized.

I've read some stuff about needing to create a custom version of my view, but I'd really rather not screw around with that because I didn't create the view programmatically and there are four or so of them.

Thanks in advance!

Foi útil?

Solução

Inside your viewController you have to override the method caneBecomeFirstResponder and answering yes, in viewDidAppear you have to set your viewController (self) to be the first responder. This is the code:

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) NSLog(@"Detected a shake");
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}


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

    [self becomeFirstResponder];
}

Outras dicas

Turns out I needed viewDidAppear, as opposed to viewDidLoad. No idea why.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top