Как бороться с UIDeviceOrientationFaceUp и UIDeviceOrientationFaceDown?
вопрос о noob.
i определите ориентацию с помощью:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
все нормально и dandy, и я изменяю свои текстовые поля и метки в соответствии с сообщенной ориентацией с помощью
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
и else {} для всего остального
проблема, которую я недавно обнаружил, - это когда UIDevice
сообщает UIDeviceOrientationFaceUp
или UIDeviceOrientationFaceDown
. как я могу справиться с этой ситуацией? как узнать, происходит ли UIDeviceOrientationFaceUp
и UIDeviceOrientationFaceDown
в портретной или альбомной ориентации? Я знаю, что устройство обращено вверх или вниз, но я не знаю, должен ли я переместить все в портрет или пейзаж.
Благодарю вас!
Ответы
Ответ 1
Apple рекомендует не использовать ориентацию устройства для макета представления. Вместо этого каждый контроллер представления имеет свойство interfaceOrientation, а UIApplication
имеет statusBarOrientation, оба из которых возвратят текущую ориентацию интерфейса, которая подходит для макета представления.
Чтобы отслеживать изменения, существуют UIViewController
методы, такие как willRotateToInterfaceOrientation: duration:, которые будут вызываться и будут уведомлены, такие как UIApplicationWillChangeStatusBarOrientationNotification, который будет опубликован при изменении ориентации интерфейса.
Ответ 2
Этот блог здесь: http://blogs.remobjects.com/blogs/mh/2010/06/29/p1685 объясняет это очень хорошо.
Ответ 3
Немного поздно, надеюсь, это поможет кому-то.
Для iOS 6.0 и более поздних версий:
1) Установите этот код во время настройки просмотра для получения уведомления о ротации:
// Set Listener to receive orientation notifications from the device
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2) Установите этот код, чтобы поймать уведомление после вращения:
-(void)detectOrientation{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationFaceDown:{
NSLog(@"UIDeviceOrientationFaceDown");
break;
}
case UIDeviceOrientationFaceUp:{
NSLog(@"UIDeviceOrientationFaceUp");
break;
}
case UIDeviceOrientationLandscapeLeft:{
NSLog(@"UIDeviceOrientationLandscapeLeft");
break;
}
case UIDeviceOrientationLandscapeRight:{
NSLog(@"UIDeviceOrientationLandscapeRight");
break;
}
case UIDeviceOrientationPortrait:{
NSLog(@"UIDeviceOrientationPortrait");
break;
}
case UIDeviceOrientationPortraitUpsideDown:{
NSLog(@"UIDeviceOrientationPortraitUpsideDown");
break;
}
case UIDeviceOrientationUnknown:{
NSLog(@"UIDeviceOrientationUnknown");
break;
}
default:{
break;
}
}
}
Ответ 4
У меня те же проблемы с
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Этот метод генерирует уведомления для 7 возможных состояний устройства:
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown
Но мне нужны только первые 4, ну это мой подход к решению этой проблемы (с включенным кодом):
Как обращаться с UIDeviceOrientation для управления макетами просмотров