如何在没有设备自动旋转的情况下检测iPhone上的旋转?

问题描述:

任何人都知道如何做到这一点?如何在没有设备自动旋转的情况下检测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

您可以通知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

您从shouldAutorotateToInterfaceOrientation:返回YES,我怀疑你不打算这么做。这是您需要实施以防止设备旋转的唯一方法。

至于获取设备的当前方向,您可以使用[[UIDevice currentDevice] orientation]

+0

嘿那里 - 谢谢你的建议! [[UIDevice currentDevice]方向]将获得当前的方向 - 但我需要检测何时发生这种情况,而无需手机自动旋转。 如果我从shouldAutorotateToInterfaceOrientation返回NO,则不会调用任何委托函数,因此我无法检测何时发生旋转。 有没有其他人有任何想法? 干杯, Nick。 – 2009-06-15 15:54:29

在旁注中shouldAutoRotateToInterfaceOrientation用于在2.x中旋转的时间上调用,但在3.0中它不太一致。另一篇文章中提到的通知是可靠指示轮换的方法。