【发布时间】:2016-05-23 07:33:49
【问题描述】:
在我的相机项目中,我想知道系统相机等用户锁定屏幕旋转的情况下的设备方向。
我如何才能知道设备方向或仅为我的项目解锁屏幕旋转??
【问题讨论】:
标签: ios camera avfoundation orientation
在我的相机项目中,我想知道系统相机等用户锁定屏幕旋转的情况下的设备方向。
我如何才能知道设备方向或仅为我的项目解锁屏幕旋转??
【问题讨论】:
标签: ios camera avfoundation orientation
您可以为此使用 CoreMotion。 签出这个库here
【讨论】:
您可能会找到几种方法来获取当前设备方向或方向变化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;
}
}
}
【讨论】: