標籤:
UIDevice類使用內建的orientation屬性擷取裝置的物理方向。IOS裝置支援這個屬性的7個可能的值。
》UIDeviceOrientationUnknown:方向目前未知。
》UIDeviceOrientationPortrait:Home鍵在下。
》UIDeviceOrientationPortraitUpsideDown:Home鍵在上
》UIDeviceOrientationLandscapeLeft:Home鍵在左邊
》UIDeviceOrientationLandscapeRight:Home鍵在右邊
》UIDeviceOrientationFaceUp:裝置正面朝上
》UIDeviceOrientationFaceDown:裝置正面朝下
IOS 提供了兩個內建宏,幫組確定裝置方向枚舉值是縱向還是橫向的:既UIDeviceOrientationIsPortrait()和UIDeviceOrientationIsLandscape().能夠很方便的擴充UIDevice類,提供這些測試作為內建的裝置屬性。
在UIDevice擴充類中添加如下代碼
@property (nonatomic , readonly)BOOL isLandscape;@property (nonatomic , readonly)BOOL isPortrait;
-(BOOL)isLandscape{ return UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation);}-(BOOL)isPortrait{ return UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation);}
代碼也可以直接訂閱裝置重新導向通知。為此,可以把beginGeneratingDeviceOrientationNotification發送給currentDevice單例。然後添加一個觀察者,捕獲隨後發生的UIDeviceOrientationDidChangeNotification更新。如你所期望的,可以通過調用UIDeviceOrientationDidChangeNotificationOrientationNotification來完成偵聽。
IOS 特定於裝置的開發:處理基本方向