How to detect orientation, if screen is locked in portrait mode?

ios objective-c iphone uiinterfaceorientation

1053 просмотра

2 ответа

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 Источник Размещён: 08.11.2019 10:57

Ответы (2)


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

Автор: Emre Gürses Размещён: 07.08.2017 01:23

-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 Размещён: 20.08.2016 06:14
Вопросы из категории :
32x32