当用户锁定屏幕旋转时,我如何以编程方式知道设备方向
how can i know the device orientation programmatically when user locked screen rotation
在我的相机项目中,我想知道在系统相机等用户锁定屏幕旋转的情况下设备方向。
我如何才能知道设备方向或解锁屏幕旋转仅为我的项目?
您可能会找到几种获取当前设备方向或方向更改的方法here
您可以为此使用 CoreMotion。
查看此库 here
感谢 Xcoder,coreMotion 帮助了我。
并遵循我的代码:
- (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;
}
}
}
在我的相机项目中,我想知道在系统相机等用户锁定屏幕旋转的情况下设备方向。
我如何才能知道设备方向或解锁屏幕旋转仅为我的项目?
您可能会找到几种获取当前设备方向或方向更改的方法here
您可以为此使用 CoreMotion。 查看此库 here
感谢 Xcoder,coreMotion 帮助了我。
并遵循我的代码:
- (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;
}
}
}