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

Nic*_*ght 2 iphone cocoa-touch rotation

有人知道怎么做吗?

我想这个:

-(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 {
}
Run Code Online (Sandbox Code Playgroud)

可能会阻止设备旋转(覆盖我的UIViewController的所有旋转方法而不是调用超类)但我担心它不是实际执行旋转的UIViewController.

我的UIViewController在UINavigationController中.

有人有主意吗?

干杯,尼克.

Nil*_*ect 20

您可以注册通知UIDeviceOrientationDidChangeNotification(从UIDevice.h),然后当您关心方向更改时,请调用以下方法:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)

如果您不再关心方向更改,请调用此方法:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)

通知将在适用时发送,您可以检查[UIDevice currentDevice].orientation以了解当前的方向.