我希望我的 iPhone 应用程序的全局行为就像只允许 UIInterfaceOrientationPortrait 一样。但同时我希望它知道物理方向,以便以特定方式做出反应,仅在显示器的有限区域内。我怎样才能得到这个?
我使用 UIViewController 的 self.interfaceOrientation iVar 做了一些测试,但这似乎不起作用,因为这个变量不会改变。
我希望我的 iPhone 应用程序的全局行为就像只允许 UIInterfaceOrientationPortrait 一样。但同时我希望它知道物理方向,以便以特定方式做出反应,仅在显示器的有限区域内。我怎样才能得到这个?
我使用 UIViewController 的 self.interfaceOrientation iVar 做了一些测试,但这似乎不起作用,因为这个变量不会改变。
首先,做[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]. 然后注册UIDeviceOrientationDidChangeNotification。当您收到通知时,请检查[UIDevice currentDevice].orientation设备的物理方向。
您绝对应该阅读这些 API 的文档,因为它包含一些重要的警告。
另请注意,设备方向返回为 a UIDeviceOrientation,与 a 不同UIInterfaceOrientation。请记住UIInterfaceOrientation文档中的以下信息:
statusBarOrientation您在属性和setStatusBarOrientation:animated:方法中使用这些常量。注意UIDeviceOrientationLandscapeRight分配给UIInterfaceOrientationLandscapeLeft和UIDeviceOrientationLandscapeLeft分配给UIInterfaceOrientationLandscapeRight; 原因是旋转设备需要向相反方向旋转内容。