[UIDevice currentDevice].orientation == UIDeviceOrientationPortrait
获取设备的方向方法1、UIDeviceOrientation是设备的方向,只能读取不能设置,
typedefNS_ENUM(NSInteger,UIDeviceOrientation){
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
// Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown,
// Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft,
// Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight,
// Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp,
// Device oriented flat, face up
UIDeviceOrientationFaceDown
// Device oriented flat, face down
};
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
设置软件方向2UIInterfaceOrientation是软件的方向,可以读取可以设置。
typedefNS_ENUM(NSInteger,UIInterfaceOrientation){
UIInterfaceOrientationPortrait
=UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown=UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft
=UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight
=UIDeviceOrientationLandscapeLeft
};
注意:UIInterfaceOrientation的横屏的左边和右边跟UIDeviceOrientation刚好相反。
2、如果需要获取设备方向变化(UIDeviceOrientation)的消息的话,需要注册UIDeviceOrientationDidChangeNotification通知。
在注册通知时,需要先调用UIDevice的beginGeneratingDeviceOrientationNotifications方法
[[UIDevicecurrentDevice]beginGeneratingDeviceOrientationNotifications];[notificationCenter addObserver:selfselector:@selector(deviceOrientationDidChange)name:UIDeviceOrientationDidChangeNotificationobject:nil];
同时,在结束时,需要移除改通知消息
[notificationCenter removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];