4

My application works in both orientation(portrait and landscape) but one of the screen is locked in portrait mode. But I have to set one value in Rotation variable for that particular screen. But I did not find orientation. So I want to find orientation. I'm using this below code for lock my screen in portrait mode and it will work.

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

I'm using this below method to detect orientation but this will not called.

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

    }
}
Yogendra Patel
  • 813
  • 2
  • 8
  • 24

2 Answers2

1

You can detect orientation, if screen is locked in portrait mode. The easy way is "CoreMotion". The code snippet is below.

1-) Firstly, you should add CoreMotion freamework to Build Settings enter image description here

2-) import CoreMotion freamework

#import <CoreMotion/CoreMotion.h>

3-) added property just like below.

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

}

4-) we are created method to initialize

 - (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-) declare outputAccelerationData method

    - (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-) call initialize method in viewDidload

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

you can see detail postchange Oriantation

Terren
  • 1,407
  • 1
  • 14
  • 21
Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
-1

here are methods to detect orientation UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait

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

// And

if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
 {
    // code here for Portrait orientation       
 }
vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • so what will you do after find the orientation after-all you locked the device orientation in portrait mode so you well knew about it now go ahead and set the variables value accordingly .. – vaibhav Aug 20 '16 at 10:44
  • Yeah I know that. But my `didRotateFromInterfaceOrientation` method not called because my screen is locked in Portrait mode. @vaibhav – Yogendra Patel Aug 22 '16 at 03:50
  • `didRotateFromInterfaceOrientation` method called when user rotate screen and you locked it earlier now you can set the value you want to set .. – vaibhav Aug 22 '16 at 06:36