私のカメラプロジェクトでは、システムカメラのようなユーザーによる画面の回転がロックされた場合のデバイスの向きを知りたい。ユーザーが画面の回転をロックしたときにデバイスの向きをプログラムで知る方法
どのようにデバイスの向きを知ることができますか、または自分のプロジェクトでのみ画面回転のロックを解除できますか?
私のカメラプロジェクトでは、システムカメラのようなユーザーによる画面の回転がロックされた場合のデバイスの向きを知りたい。ユーザーが画面の回転をロックしたときにデバイスの向きをプログラムで知る方法
どのようにデバイスの向きを知ることができますか、または自分のプロジェクトでのみ画面回転のロックを解除できますか?
を取得するためのいくつかの方法を見つけることができます。 このライブラリをチェックアウト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;
}
}
}
このdoesnの 'tの仕事、画面回転ロック、UIDevice.currentDevice()の向きは、前の状態なかれ場合であり、我々は回転動作にこれをキャプチャすることはできません時間。 – zhiyi
CoreMotionが役立ちます – zhiyi