简体   繁体   中英

how can i know the device orientation programmatically when user locked screen rotation

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??

您可能会发现几个方法来得到当前设备的方向或方向改变这里

You can use CoreMotion for this. Checkout this library here

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;
        }
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM