web-dev-qa-db-fra.com

Comment obtenir l'orientation actuelle des iPhones?

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 retourne un objet UIInterfaceOrientation. J'en ai juste besoin pour une construction if-else comme

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

Merci d'avance!

43
Knodel

C'est probablement ce que vous voulez:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

Vous pouvez ensuite utiliser des macros système comme:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{

}

Si vous souhaitez utiliser l'orientation de l'appareil:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

Cela inclut des énumérations comme UIDeviceOrientationFaceUp et UIDeviceOrientationFaceDown

114
Satyajit

Comme indiqué dans d'autres réponses, vous avez besoin de l'interfaceOrientation, pas du deviceOrientation.

La façon 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

Rappel: L'orientation gauche est entrée en tournant votre appareil vers la droite.

11
Bjinse

Voici un extrait de code que je n'ai pas envie d'écrire parce que des problèmes bizarres revenaient dans ma vue racine lorsque l'orientation a été modifiée .... Je vois que vous appelez simplement la méthode qui aurait dû être appelée mais qui semblait l'être. ... cela fonctionne très bien sans erreurs

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

UIInterfaceOrientation est maintenant obsolète, et UIDeviceOrientation inclut UIDeviceOrientationFaceUp et UIDeviceOrientationFaceDown donc ne peut pas être utilisé pour vous donner l'orientation de l'interface.

Mais la solution est assez simple

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