2016-08-20 5 views
2

Мое приложение работает как в ориентации (портретной, так и альбомной), но один из них заблокирован в портретном режиме. Но я должен установить одно значение в переменной вращения для этого конкретного экрана. Но я не нашел ориентации. Итак, я хочу найти ориентацию. Я использую этот ниже код для блокировки своего экрана в портретном режиме, и он будет работать.Как определить ориентацию, если экран заблокирован в портретном режиме?

- (UIInterfaceOrientationMask) supportedInterfaceOrientations { 
    [super supportedInterfaceOrientations]; 
    return UIInterfaceOrientationMaskPortrait; 
} 

Я использую этот метод ниже для определения ориентации, но это не будет вызвано.

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

    } 
} 
+0

Вы можете увидеть ссылку. http://stackoverflow.com/questions/9122149/detecting-ios-uidevice-orientation –

ответ

0

здесь методы обнаружения ориентации UIDeviceOrientationIsLandscape и UIDeviceOrientationIsPortrait

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

// And 

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

, так что вы будете делать после того, как найдете ориентацию после того, как вы заблокировали ориентацию устройства в портретном режиме, чтобы вы знали об этом сейчас. задайте значения переменных соответственно. – vaibhav

+0

Да, я знаю это. Но метод 'didRotateFromInterfaceOrientation' не вызван, потому что мой экран заблокирован в режиме Portrait. @vaibhav –

+0

метод 'doneRotateFromInterfaceOrientation' вызывается, когда пользователь поворачивает экран, и вы заблокировали его ранее, теперь вы можете установить значение, которое вы хотите установить. – vaibhav

0

Вы можете обнаружить ориентацию, если экран заблокирован в портретном режиме. Простым способом является «CoreMotion». Ниже приведен фрагмент кода.

1) Во-первых, вы должны добавить CoreMotion freamework Построить Настройки enter image description here

2-) импорт CoreMotion freamework

#import <CoreMotion/CoreMotion.h> 

3-) добавлено свойство так же, как ниже.

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

} 

4-) мы создали метод для инициализации

- (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-) объявить 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-) вызов метода инициализации в viewDidload

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

вы можете увидеть подробное сообщение change Oriantation