motionEnded getting called multiple times
I have a UIViewController subclass that I am trying to have handle the shake event when its view is up.
Here are the relevant methods I've implemented:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventTypeMotion && event.type == UIEventSubtypeMotionShake) {
NSLog(@"%@ motionEnded", [NSDate date]);
}
开发者_JAVA技巧if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
[super motionEnded:motion withEvent:event];
}
}
You would expect that when I hit ^+Cmd+Z in the iPhone Simulator that it would just log once, but it is consistently logging twice for each event. Below is the result of three "shake" simulations:
2009-10-09 20:52:06.216 TestApp[39802:20b] 2009-10-09 20:52:06 -0400 motionEnded
2009-10-09 20:52:06.218 TestApp[39802:20b] 2009-10-09 20:52:06 -0400 motionEnded 2009-10-09 20:52:07.689 TestApp[39802:20b] 2009-10-09 20:52:07 -0400 motionEnded 2009-10-09 20:52:07.690 TestApp[39802:20b] 2009-10-09 20:52:07 -0400 motionEnded 2009-10-09 20:52:08.001 TestApp[39802:20b] 2009-10-09 20:52:08 -0400 motionEnded 2009-10-09 20:52:08.002 TestApp[39802:20b] 2009-10-09 20:52:08 -0400 motionEndedHas anyone seen this and, if so, how did you fix it? I'm using iPhone SDK 3.1 and Xcode Version 3.1.4.
Here what I have discovered, looks like a sim bug to me:
- Issue (double motionEnded notification) happens when target is OS 3.1 and 3.1.0 on sim
- Issue DOES not happen when target is 3.0 on sim
Issue NEVER happens on actual device regardless of the target.
so this must be a sim bug. When I have a chance I will submit as a bug to apple w/repro
Haven't seen this, but you might want to try it without invoking the super method. The default implementation of motionEnded
(from UIResponder) is supposed to be a NOP so there's no need to call the parent method.
Also, have you tried this on the device itself? It could be a simulator issue.
精彩评论