Shake recognition not working on iOS 4, does work on iOS 5

折月煮酒 提交于 2020-01-04 07:02:15

问题


I've integrated a shake recognition in my app. I've put it in my appdelegate to be able to use it throughout the app. It works great on iOS 5, but on iOS 4 it doesn't work.

I'm using the following code in my appdelegate.m:

- (void)applicationDidBecomeActive:(UIApplication *)application {
   [self becomeFirstResponder];
   ....
}

-(BOOL)canBecomeFirstResponder {
   return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"motionBegan");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"motionEnded");
}

If I run this in the iOS5 simulator and activate the shake gesture I get the NSLog messages. If I run it in the iOS 4.3 simulator it doesn't work. Same thing with real devices.


回答1:


I had the same issue. Try

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

instead of

- (void)applicationDidBecomeActive:(UIApplication *)application {
   [self becomeFirstResponder];
   ....
}

as in Event Handling Guide: Motion Events. This worked for me.



来源:https://stackoverflow.com/questions/8458616/shake-recognition-not-working-on-ios-4-does-work-on-ios-5

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