问题
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