2009-06-15 39 views
2

任何人都知道如何做到這一點?如何在沒有設備自動旋轉的情況下檢測iPhone上的旋轉?

我想這:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

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

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
} 

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
} 

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
} 

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration { 
} 

可以防止旋轉裝置(覆蓋所有轉動我的UIViewController的方法,而不是調用父類),但我擔心它不是實際執行旋轉的UIViewController。

我的UIViewController位於UINavigationController中。

任何人有任何想法?

乾杯, Nick。

+1

實際上,您不應該重寫所有那些空的委託方法,而只需要實際需要使用的方法。無論您是否定義它,調用者都應該測試您的子類在調用它之前是否響應某個特定的方法。這只是代表工作的方式。 – 2009-06-15 17:01:41

回答

20

您可以通知UIDeviceOrientationDidChangeNotification(從UIDevice.h)註冊,然後當你關心的方向變化調用這個方法:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

當你不再關心方位的變化,調用此方法:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 

通知將在適用時發送,您可以檢查[UIDevice currentDevice].orientation以瞭解當前方向。

+0

謝謝 - 這正是我所追求的! – 2009-06-15 16:54:25

+0

不好的副作用是設備不會再進入睡眠狀態。 – drvdijk 2009-08-06 14:59:03

1

您從shouldAutorotateToInterfaceOrientation:返回YES,我懷疑你不打算這麼做。這是您需要實施以防止設備旋轉的唯一方法。

至於獲取設備的當前方向,您可以使用[[UIDevice currentDevice] orientation]

+0

嘿那裏 - 謝謝你的建議! [[UIDevice currentDevice]方向]將獲得當前的方向 - 但我需要檢測何時發生這種情況,而無需手機自動旋轉。 如果我從shouldAutorotateToInterfaceOrientation返回NO,則不會調用任何委託函數,因此我無法檢測何時發生旋轉。 有沒有其他人有任何想法? 乾杯, Nick。 – 2009-06-15 15:54:29

4

在旁註中shouldAutoRotateToInterfaceOrientation用於在2.x中旋轉的時間上調用,但在3.0中它不太一致。另一篇文章中提到的通知是可靠指示輪換的方法。