2010-08-20 85 views
2

我想我已經做了所有我應該檢測到一個動搖,但motionEnded:withEvent:永遠不會被調用。 (一起皺紋的是,我沒有UIViewController - 我的應用程序是基於「OpenGL ES的應用程序」模板。)motionEnded沒有被調用(沒有視圖控制器)

我添加application.applicationSupportsShakeToEdit = YES;application:didFinishLaunchingWithOptions:,並

- (BOOL)canBecomeFirstResponder { return YES; } 

到EAGLView .m(它確實叫)和[self becomeFirstResponder];initWithCoder:(並且也嘗試了其他各種地方)。

但調試嗟

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 

我丟失了一些步驟?我必須有控制器嗎?

(我在iPad模擬器使用的是iOS 3.2。)

回答

0

更新到最新版本的SDK魔術般修復了這個問題。 [Shrug]

0

不能成爲第一個響應之前,你的看法是視圖層次結構。

+0

我打過電話是別的地方太多,但它似乎並沒有幫助 - 就像在'drawView'。 – Grumdrig 2010-08-21 20:28:20

+0

你可以創建一個視圖控制器來粘貼EAGLView,並在viewDidAppear中調用becomeFirstResponder:? – 2010-08-23 01:56:29

+0

我很想盡量避免,如果我可以。如果我沒有弄錯,UIView是'UIResponder's。 – Grumdrig 2010-08-24 18:09:46

1

UIResponder鏈與振動通知一起使用的方式令人討厭。似乎UIWindow總是得到通知,然後子響應者可能會或可能不會依賴於鏈中上面的內容。我創建了一個UIWindow子類,並將其定義爲我用下面的窗口類:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (event.type == UIEventSubtypeMotionShake) 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtypeMotionShakeEnded" object:nil]; 
} 

然後,對於想搖通知的任何意見,我只是讓他們自己添加爲觀察員,UIEventSubtypeMotionShakeEnded事件,他們每次都得到它。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeNotification:) 
              name:@"UIEventSubtypeMotionShakeEnded" object:nil]; 
+0

一個更深入的答案將在http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone通過Joe D'Andrea – LolaRun 2011-06-08 07:45:23

+0

您是否注意到您驗證了事件.type'反對'UIEventSubtypeMotionShake'這是一個子類型?巧合的是,這是因爲'UIEventSubtypeMotionShake'和'UIEventTypeMotion'具有相同的值。 – bio 2015-12-14 15:02:25

2

您必須添加到您的控制器:

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

-(BOOL)becomeFirstResponder 
{ 
    return YES; 
} 
+0

這是正確的方法。它爲我工作。我遇到了問題,因爲我的視圖在導航視圖控制器中 – Markicevic 2017-05-09 11:31:45

相關問題