[UIDevice currentDevice].orientation
一般情況下可通過以上代碼獲取設(shè)備朝向,但是當(dāng)設(shè)備開啟《屏幕旋轉(zhuǎn)鎖定》開關(guān)后則拿不到準(zhǔn)確的設(shè)備方向了
//通過設(shè)備運動監(jiān)視器來監(jiān)聽
//初始化
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
//設(shè)置更新頻率,默認值為0.01,就算設(shè)置為0也是0.01,值越小更新越快
motionManager.deviceMotionUpdateInterval = 0.3;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x)) {
if (y >= 0) {
//UIDeviceOrientationPortraitUpsideDown
}else {
//UIDeviceOrientationPortrait
}
}else {
if (x >= 0) {
//UIDeviceOrientationLandscapeRight
}else {
//UIDeviceOrientationLandscapeLeft
}
}
}];
這里通過監(jiān)聽設(shè)備運動來識別設(shè)備朝向,也可通過重力感應(yīng)來監(jiān)聽,但重力感應(yīng)監(jiān)聽在設(shè)備水平拿的時候得到的是 faceUp/faceDown,如果此朝向需求用于攝像頭方向設(shè)定還是用以上監(jiān)聽比較準(zhǔn)確