Подтвердить что ты не робот

Как получить текущую ориентацию iPhone?

Есть ли специальный метод для ориентации iPhone? Мне это не нужно в градусах или радианах, я хочу, чтобы он возвращал объект UIInterfaceOrientation. Мне просто нужно это для построения if-else вроде

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

Спасибо заранее!

4b9b3361

Ответ 1

Скорее всего, вы хотите:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

Затем вы можете использовать системные макросы, например:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{

}

Если вы хотите, чтобы ориентация устройства использовалась:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

Это включает перечисления типа UIDeviceOrientationFaceUp и UIDeviceOrientationFaceDown

Ответ 2

Как обсуждалось в других ответах, вам нужна интерфейсOrientation, а не deviceOrientation.

Самый простой способ добраться до этого - использовать интерфейс свойствОриентации на вашем UIViewController. (Поэтому чаще всего просто: self.interfaceOrientation будет делать).

Возможные значения:

UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

Помните: Левая ориентация вводится путем поворота устройства вправо.

Ответ 3

Вот фрагмент кода, который я написал, потому что у меня возникали странные проблемы, возвращающиеся в мой корневой вид, когда ориентация была изменена... Я вижу, что вы просто вызываете метод, который должен был быть вызван, но казался быть.... это отлично работает без ошибок

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
        NSLog(@"landscape left");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
        NSLog(@"landscape right");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
        //do something or rather
        [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
        NSLog(@"portrait");
    }
}

Ответ 4

UIInterfaceOrientation теперь устарел, а UIDeviceOrientation включает UIDeviceOrientationFaceUp и UIDeviceOrientationFaceDown, поэтому нельзя полагаться на ориентацию интерфейса.

Решение довольно просто, хотя

if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
    // Landscape
} else {
    // Portrait
}