我有一個UIViewController子類,我試圖在視圖啓動時處理shake事件。motionEnded被多次調用
下面是相關的方法我已經實現:
- (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];
}
}
你會想到,當我打^ + Cmd的+ Z在iPhone模擬器,它將只需登錄一次,但它始終兩次記錄爲每個事件。下面是三個「搖動」模擬的結果:
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 motionEnded
有沒有人看過這個,如果是的話,你是如何解決它的?我正在使用iPhone SDK 3.1和Xcode版本3.1.4。
你們每個人都弄明白了嗎?我有同樣的問題 – Mark