UIDeviceOrientationFaceUp - 如何区分纵向和横向?
我试图找出设备是否处于纵向或横向模式。如果设备不朝上,我的代码工作得很好。如果它面朝上(并且方向== 5),则它将不区分纵向和横向。无论如何,如果UIDeviceOrientation是FaceUp,那么根据横向/纵向来确定“定位”?UIDeviceOrientationFaceUp - 如何区分纵向和横向?
我的代码:
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
NSLog(@"orientation: %d", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
NSLog(@"LANDSCAPE!!!");
}
if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
NSLog(@"PORTRAIT!!!");
}
你不应该混淆UIDeviceOrientation
和UIInterfaceOrientation
,它们是不同的,但相关的如通过其声明
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
UIDeviceOrientation
告诉你的设备的方向是什么。 UIInterfaceOrientation
告诉你界面的方向是什么,并且被UIViewController
使用。 UIInterfaceOrientation
显然是纵向或横向,而UIDeviceOrientation
可能有不明确的值(UIDeviceOrientationFaceUp
,UIDeviceOrientationFaceDown
,UIDeviceOrientationUnknown
)。
在任何情况下,你不应该试图确定一个UIViewController的方位[[UIDevice currentDevice] orientation]
,因为不管是什么设备方向是UIViewController interfaceOrientation
属性可以是不同的(例如,如果您的应用程序不旋转为横向的所有[[UIDevice currentDevice] orientation]
能是UIDeviceOrientationLandscapeLeft
,而viewController.interfaceOrientation
可以是UIInterfaceOrientationPortrait
)。
更新: 作为iOS的8.0,[UIViewController interfaceOrientation]
已弃用。另一种提供here的替代品是[[UIApplication sharedApplication] statusBarOrientation]
。这也返回UIInterfaceOrientation
。
我有这样的代码框架来处理想&不必要的设备的方位,在我的情况,我想忽略UIDeviceOrientationUnknown,UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,缓存最后允许的方向。此代码涉及iPhone和iPad设备,可以帮助您。
- (void)modXibFromRotation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *device = [[UIDevice currentDevice]localizedModel];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if ([device isEqualToString:@"iPad"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
}