2010-03-25 9 views
42

Existe-t-il une méthode spéciale pour obtenir l'orientation des iPhones? Je n'en ai pas besoin en degrés ou en radians, je veux qu'il renvoie un objet UIInterfaceOrientation. Je l'ai juste besoin pour une construction if-else commeComment obtenir l'orientation actuelle des iPhones?

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

Merci d'avance!

Répondre

110

Ceci est probablement ce que vous voulez:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

Vous pouvez utiliser des macros du système comme:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
{ 

} 

Si vous voulez l'utilisation d'orientation de l'appareil:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

Cette comprend des énumérations comme UIDeviceOrientationFaceUp et UIDeviceOrientationFaceDown

+0

Ouais, ça a marché! Que vous beaucoup! – Knodel

+29

C'est techniquement faux, vous obtenez un UIDeviceOrientation de votre appel de méthode et le stockez dans un UIInterfaceOrientation. Un UIInterfaceOrientation est l'un des 4, et représente ce à quoi ressemble votre interface. Le UIDeviceOrientation est l'un des 6 (pose à plat et à l'envers) et vous indique l'orientation de l'appareil, pas nécessairement à quoi ressemble votre interface. Votre interface pourrait être en mode portrait mais si le périphérique est assis sur une table, il retournera UIDeviceOrientationFaceUp. –

+12

@CoryImdieke: Merci de mettre à jour la différence entre l'orientation de l'appareil et de l'interface. UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; est l'API correcte – Senthil

11

Comme nous l'avons vu dans d'autres réponses, vous avez besoin de l'interfaceOrientation, pas du deviceOrientation. La manière la plus simple d'y arriver est d'utiliser la propriété interfaceOrientation sur votre UIViewController. (Donc le plus souvent, juste: self.interfaceOrientation fera l'affaire).

Les valeurs possibles sont:

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

Rappelez-vous: orientation à gauche est entré en tournant votre appareil vers la droite.

+0

Dommage qu'il soit désormais obsolète. – Rivera

+0

Ouais c'est déprécié, que faisons-nous maintenant? – trapper

4

Voici un extrait de code que j'ai eu à écrire parce que je recevais des problèmes bizarres qui revenaient dans ma vue racine quand l'orientation était changée ... Je vois juste appeler la méthode qui aurait dû être appelée mais semblent être .... cela fonctionne très bien aucune erreur

- (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

Cela ne fonctionnera pas pour 'UIDeviceOrientationFaceUp' et' UIDeviceOrientationFaceDown' – trapper

1

UIInterfaceOrientation est maintenant dépréciée, et UIDeviceOrientation comprend UIDeviceOrientationFaceUp et UIDeviceOrientationFaceDown ne peut donc pas être invoqué pour vous donner l'orientation de l'interface.

La solution est assez simple si

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