2010-03-25 22 views
42

¿Hay algún método especial para obtener la orientación de los iPhones? No lo necesito en grados o radianes, quiero que devuelva un objeto UIInterfaceOrientation. Sólo lo necesito para una construcción if-else como¿Cómo obtener la orientación actual de iPhones?

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

¡Gracias de antemano!

Respuesta

110

Esto es más probable lo que quiere:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

A continuación, puede utilizar macros sistema como:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
{ 

} 

Si desea que el uso de la orientación del dispositivo:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

Este incluye enumeraciones como UIDeviceOrientationFaceUp y UIDeviceOrientationFaceDown

+0

Sí, funcionó! ¡Que mucho! – Knodel

+29

Esto es técnicamente incorrecto, obtiene una orientación de UIDevice de su llamada de método y la almacena en una UIInterfaceOrientation. Una UIInterfaceOrientation es una de las 4 y representa cómo se ve tu interfaz. UIDeviceOrientation es uno de los 6 (incluye tendido plano y acostado boca abajo) y le indica la orientación del dispositivo, no necesariamente la apariencia de su interfaz. Su interfaz podría estar en modo vertical, pero si el dispositivo está sobre una mesa, devolvería UIDeviceOrientationFaceUp. –

+12

@CoryImdieke: Gracias por actualizar la diferencia entre la orientación del dispositivo y la interfaz. UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; es la API correcta – Senthil

11

Como se discutió en otras respuestas, necesita la interfazOrientación, no la orientación del dispositivo.

La forma más fácil de llegar a esto es utilizar la propiedad interfaceOrientation en su UIViewController. (Por lo tanto, la mayoría de las veces, solo: self.interfaceOrientation lo hará).

Los valores posibles son:

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

Recuerde: orientación izquierda se introduce cuando gira el dispositivo hacia la derecha.

+0

Lástima que ahora está en desuso. – Rivera

+0

Sí, esto está en desuso, ¿qué hacemos ahora? – trapper

4

Aquí hay un fragmento de código que tengo que escribir porque estaba volviendo a tener problemas extraños cuando volví a la vista raíz cuando cambié la orientación ... Veo que acaba de llamar el método que debería haberse llamado pero que se hizo parecen ser .... esto funciona muy bien sin errores

- (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"); 
    } 
} 
+0

Esto no funcionará para 'UIDeviceOrientationFaceUp' y' UIDeviceOrientationFaceDown' – trapper

1

UIInterfaceOrientation ahora está en desuso, y UIDeviceOrientation incluye UIDeviceOrientationFaceUp y UIDeviceOrientationFaceDown por lo que no se puede confiar en para darle la orientación interfaz.

La solución es bastante simple, aunque

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