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!

有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top