iPhone Shake event not properly working

邮差的信 提交于 2019-12-11 02:52:04

问题


I have this inside my viewController:

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

    if (event.type == UIEventSubtypeMotionShake) {

        NSLog(@"I have shaked");

    }
}

Why is this not working? Edit:


I do infact have this:

- (void) viewWillAppear:(BOOL)animated
{
    [shakeView becomeFirstResponder];
    [super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
    [shakeView resignFirstResponder];
    [super viewWillDisappear:animated];
}

回答1:


If the UIViewController is loaded at the very start of the application, I've seen an odd glitch in OS 3.0 where it would not become the first responder unless you delayed the appropriate message a bit. Try placing

[self performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.3];

within -loadView or something else that is called when the controller is first set up.

There may be a more elegant way to work around this, but this approach has worked for me.




回答2:


The viewController must be the first responder during the shake to receive this event.

This could be one reason it's not working.



来源:https://stackoverflow.com/questions/1984087/iphone-shake-event-not-properly-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!