2016-08-20 2 views
2

Mon application fonctionne dans les deux sens (portrait et paysage) mais l'un des écrans est verrouillé en mode portrait. Mais je dois définir une valeur dans la variable Rotation pour cet écran particulier. Mais je n'ai pas trouvé d'orientation. Donc, je veux trouver une orientation. J'utilise ce code ci-dessous pour verrouiller mon écran en mode portrait et cela fonctionnera. J'utilise cette méthode ci-dessous pour détecter l'orientation, mais cela ne sera pas appelé.Comment détecter l'orientation, si l'écran est verrouillé en mode portrait?

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"UIInterfaceOrientationPortrait"); 
    } 
    else 
    { 
     NSLog(@"UIInterfaceOrientationland"); 

    } 
} 
+0

Vous pouvez voir la référence. http://stackoverflow.com/questions/9122149/detecting-ios-uidevice-orientation –

Répondre

0

ici sont des méthodes pour détecter l'orientation UIDeviceOrientationIsLandscape et UIDeviceOrientationIsPortrait

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) 
{ 
    // code here for landscape orientation  
} 

// And 

if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
{ 
    // code here for Portrait orientation  
} 
+0

Alors que ferez-vous après avoir trouvé l'orientation après tout, vous avez verrouillé l'orientation de l'appareil en mode portrait, donc vous le savez bien maintenant allez-y et Réglez la valeur des variables en conséquence .. – vaibhav

+0

Oui, je le sais. Mais ma méthode 'didRotateFromInterfaceOrientation' n'est pas appelée parce que mon écran est verrouillé en mode portrait. @vaibhav –

+0

méthode 'didRotateFromInterfaceOrientation' appelée lorsque l'utilisateur tourne l'écran et vous l'avez verrouillé plus tôt maintenant, vous pouvez définir la valeur que vous souhaitez définir .. – vaibhav

0

Vous pouvez détecter l'orientation, si l'écran est verrouillé en mode portrait. Le moyen le plus simple est "CoreMotion". L'extrait de code est ci-dessous.

1-) Tout d'abord, vous devez ajouter CoreMotion freamework construire des paramètres enter image description here

2-) importation CoreMotion freamework

#import <CoreMotion/CoreMotion.h> 

3-) ajouté propriété comme ci-dessous.

@interface BaseGeneralViewController(){ 
    UIInterfaceOrientation orientationLast, orientationAfterProcess; 
    CMMotionManager *motionManager; 
    UIInterfaceOrientation orientationNew; 

} 

4-) on créé procédé pour initialiser

- (void)initializeMotionManager{ 
    motionManager = [[CMMotionManager alloc] init]; 
    motionManager.accelerometerUpdateInterval = .2; 
    motionManager.gyroUpdateInterval = .2; 

    [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
             withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
              if (!error) { 
               [self outputAccelerationData:accelerometerData.acceleration]; 
              } 
              else{ 
               NSLog(@"%@", error); 
              } 
             }]; 
} 

5-) déclarer procédé outputAccelerationData

- (void)outputAccelerationData:(CMAcceleration)acceleration{ 

    if (acceleration.x >= 0.75 && orientationLast != UIInterfaceOrientationLandscapeLeft) { 
     orientationNew = UIInterfaceOrientationLandscapeLeft; 
     [self callQRCodePayment:UIDeviceOrientationLandscapeRight]; 
    } 
    else if (acceleration.x <= -0.75 && orientationLast != UIInterfaceOrientationLandscapeRight) { 
     orientationNew = UIInterfaceOrientationLandscapeRight; 
     [self callQRCodePayment:UIDeviceOrientationLandscapeLeft]; 
    } 
    else if (acceleration.y <= -0.75) { 
     //orientationNew = UIInterfaceOrientationPortrait; 
     //NSLog(@"Portrait"); 
    } 
    else if (acceleration.y >= 0.75) { 
     //orientationNew = UIInterfaceOrientationPortraitUpsideDown; 
    } 
    else { 
     // Consider same as last time 
     return; 
    } 

    if (orientationNew == orientationLast) 
     return; 

    orientationLast = orientationNew; 
} 

6-) appeler initialiser procédé viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self initializeMotionManager]; 
} 

vous pouvez voir le détail poste change Oriantation