本节书摘来自异步社区《iOS 6高级开发手册(第4版)》一书中的第1章,第1.6节处理基本的方向,作者 【美】Erica Sadun,更多章节内容可以访问云栖社区“异步社区”公众号查看
1.6 处理基本的方向iOS 6高级开发手册(第4版)UIDevice类使用内置的orientation属性获取设备的物理方向。iOS设备支持这个属性的7个可能的值。
UIDeviceOrientationUnknown`:向目前未知。UIDeviceOrientationPortrait:Home``键按下。UIDeviceOrientationPortraitUpsideDown:Home键升起。UIDeviceOrientationLandscapeLeft:Home键在左边。UIDeviceOrientationLandscapeRight:Home键在右边。UIDeviceOrientationFaceUp:设备正面朝上。UIDeviceOrientationFaceDown:设备正面朝下。设备在典型的应用程序会话期间可能经历其中任何或所有的方向。尽管创建的方向要与机载加速计保持一致,但是不会以任何方式把这些方向绑定到内置的角度值。
iOS提供了两个内置宏,帮助确定设备方向枚举值是纵向还是横向的:即UIDevice- OrientationIsPortrait()和UIDeviceOrientationIsLandscape()。能够很方便地扩展UIDevice类,提供这些测试作为内置的设备属性:
@property (nonatomic, readonly) BOOL isLandscape; @property (nonatomic, readonly) BOOL isPortrait; - (BOOL) isLandscape { return UIDeviceOrientationIsLandscape(self.orientation); } - (BOOL) isPortrait { return UIDeviceOrientationIsPortrait(self.orientation); }你的代码可以直接订阅设备重定向通知。为此,可以把beginGeneratingDeviceOrientation Notifications发送给currentDevice单例。然后添加一个观察者,捕获随后发生的UIDevice- OrientationDidChangeNotification更新。如你所期望的,可以通过调用UIDeviceOrientationDid- ChangeNotificationOrientationNotification来完成侦听。