2

Disclaimer :This is not a duplicate of any question.I have gone through this question before posting here.

Normally we use device orientation property or status bar property to find out the actual orientation of the device. But i am not getting the correct orientation when orientation lock is enabled on the device. I am always getting the orientation value as portrait even though i turn the device into landscape mode.

Is there a way to find out the actual orientation of the device when the orientation lock is enabled on the device.

Note : UIAccelerometer is now deprecated. Any help with respect to CoreMotion is welcome.

Community
  • 1
  • 1
A for Alpha
  • 2,904
  • 8
  • 42
  • 76
  • I don't have an answer sorry. But i thought i'd point out/gently remind that there is both deviceOrientation and interfaceOrientation. It seems to me that the 'rotation lock' is probably the main reason (along with those apps that don't support rotation) that there need be two separate concepts rather than the one eh :) – Jef Dec 31 '14 at 11:10
  • you already answer your own question. You can use CoreMotion to work out the device orientation. Specifically, you need the pitch, roll and yaw values – Tien Dinh Dec 31 '14 at 21:27
  • The problem with Core motion is that there are too many orientation values. – A for Alpha Jan 02 '15 at 06:04

1 Answers1

4

Here is what I came up with:

CMMotionManager *cm=[[CMMotionManager alloc] init];
cm.deviceMotionUpdateInterval=0.2f;
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                        withHandler:^(CMDeviceMotion *data, NSError *error) {

                            if(fabs(data.gravity.x)>fabs(data.gravity.y)){
                                    NSLog(@"LANSCAPE");

                                    if(data.gravity.x>=0){
                                        NSLog(@"LEFT");
                                    }
                                    else{
                                        NSLog(@"RIGHT");
                                    }
                                    
                            }
                            else{
                                    if(data.gravity.y>=0){
                                        NSLog(@"DOWN");
                                    }
                                    else{

                                        NSLog(@"UP");
                                    }
                                    
                            }

}];
FlySoFast
  • 1,854
  • 6
  • 26
  • 47
  • if device is move from landscape to portrait.. how can i get actual value – neha Jun 24 '20 at 07:52
  • @neha I'm not sure what you meant by "actual value". Take the code snippet above as the reference, if it prints out "LEFT" or "RIGHT", that's landscape. Otherwise it means portrait. – FlySoFast Jun 26 '20 at 06:27