UIDeviceOrientationFaceUp - как отличить портрет и пейзаж?
Я пытаюсь выяснить, находится ли устройство в портретном или альбомном режиме. Мой код работает очень хорошо, если устройство не обращено вверх. Если он работает лицом вверх (и ориентацией == 5), он не будет различать портрет и пейзаж. Нужно ли вообще определять "ориентацию" в плане пейзажа/портрета, если UIDeviceOrientation является FaceUp?
Мой код:
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
NSLog(@"orientation: %d", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
NSLog(@"LANDSCAPE!!!");
}
if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
NSLog(@"PORTRAIT!!!");
}
Ответы
Ответ 1
Не следует путать 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
).
Update:
Начиная с iOS 8.0, [UIViewController interfaceOrientation]
устарел. Альтернативой, предлагаемой здесь, является [[UIApplication sharedApplication] statusBarOrientation]
. Это также возвращает UIInterfaceOrientation
.
Ответ 2
Я делаю этот скелет кода для работы с желаемыми и нежелательными ориентациями устройств, в моем случае я хочу игнорировать 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 */
}
}
}