我有一个 UIViewController 子类,我试图在它的视图启动时处理抖动事件。
以下是我实施的相关方法:
- (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]);
}
if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
[super motionEnded:motion withEvent:event];
}
}
您会期望,当我在 iPhone 模拟器中点击 ^+Cmd+Z 时,它只会记录一次,但它会始终为每个事件记录两次。以下是三个“摇晃”模拟的结果:
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 运动结束
有没有人看到这个,如果有,你是如何解决的?我正在使用 iPhone SDK 3.1 和 Xcode 版本 3.1.4。