0

In my camera project, i want to know the device orientation in the case of locked screen rotation by user like the system camera.

how can i know the device orientation or unlock the screen rotation only for my project??

zhiyi
  • 13
  • 4

3 Answers3

0

You may find several methods to get the current device orientation or orientation change here

Community
  • 1
  • 1
Mathews
  • 733
  • 5
  • 11
  • this doesn`t work ,when the screen rotation locking,UIDevice.currentDevice().orientation is alway case the status before, and we can not capture the rotation action this time. – zhiyi May 28 '16 at 09:52
  • CoreMotion can help – zhiyi May 28 '16 at 09:55
0

You can use CoreMotion for this. Checkout this library here

Xcoder
  • 1,762
  • 13
  • 17
0

Thanks for Xcoder, coreMotion helped me.

And following my code:

- (void)startMotionManager{
    if (_motionManager == nil) {
        _motionManager = [[CMMotionManager alloc] init];
    }
    _motionManager.deviceMotionUpdateInterval = 1/2.0;
    if (_motionManager.deviceMotionAvailable) {
        NSLog(@"Device Motion Available");
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler: ^(CMDeviceMotion *motion, NSError *error){
            [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
        }];
    } else {
        NSLog(@"No device motion on device.");
        [self setMotionManager:nil];
    }
}

- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion {
    double x = deviceMotion.gravity.x;
    double y = deviceMotion.gravity.y;
    if (fabs(y) >= fabs(x)) {
        if (y >= 0) {
            self.deviceOrientation = UIDeviceOrientationPortraitUpsideDown;
        } else {
            self.deviceOrientation = AVCaptureVideoOrientationPortrait;
        }
    } else {
        if (x >= 0) {
            self.deviceOrientation = UIDeviceOrientationLandscapeRight;
        }
        else {
            self.deviceOrientation = UIDeviceOrientationLandscapeLeft;
        }
    }
} 
zhiyi
  • 13
  • 4