2016-08-20 117 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

這裏有檢測方向UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait

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'方法沒有被調用,因爲我的屏幕在肖像模式下被鎖定。 @vaibhav –

+0

'didRotateFromInterfaceOrientation'方法在用戶旋轉屏幕時調用,並且您現在先鎖定它,現在您可以設置要設置的值。 – 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

相關問題