2014-07-24 51 views
1

我在willRotateToInterfaceOrientation:方法中有一些代碼。但是在方向改變後它會被調用。將設備旋轉後調用willRotateToInterfaceOrientation

而且我想在取向改變之前調用這個方法。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
//some code 
} 

人知道爲什麼會出現這種情況?

+0

設備只知道在用戶實際旋轉設備後旋轉方向,因此無法預測方向會改變,直到用戶實際旋轉設備。 –

+0

但是在文檔支持中,他們編寫的willRotateToInterfaceOrientation()會在方向更改之前調用... – Saggy

+0

是的,在界面方向更改之前,不是在設備方向更改之前。 –

回答

0

我有同樣的問題,但發現周圍的一種方式。剛開始聽設備方向事件:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver: self 
             selector: @selector(detectOrientation) 
              name: UIDeviceOrientationDidChangeNotification 
              object: nil]; 

你的處理器(detectOrientation在上面的例子中)將被稱爲以前-willRotateToInterfaceOrientation:toInterfaceOrientationduration:方法,因此也前的畫面在新的方向呈現。

請注意,每次撥打beginGeneratingDeviceOrientationNotifications必須匹配endGeneratingDeviceOrientationNotificationsas noted in the docs)的呼叫。因此,如果您撥打-viewDidLoad的第一個號碼,請不要忘記撥打dealloc

還有一點需要注意的是,這種方法會打開加速度計硬件,因此您的應用程序可能會消耗更多電量,但它仍然有效(也就是說,直到您致電endGeneratingDeviceOrientationNotifications)。