iOS 橫屏鎖
事發(fā):朋友公司是做教育的,APP中講課畫板是需要在橫屏過程中全屏顯示,自己已經(jīng)實現(xiàn)橫豎屏切換,但是老板有這樣一個需求,假如用戶開啟了橫屏鎖,需要提醒用戶將橫屏鎖打開。于是找到了我,問我有沒有系統(tǒng)提供的一個API供我們調用,如果有,就直接調用解決了。
我在查了好多文檔和Google后發(fā)現(xiàn)官方并沒有提供這樣的接口,這里我貼出來一個鏈接可以看看,說的不錯,Detect iOS device orientation lock.
于是我換了種思路來解決這個問題。思路是這樣的,利用重力感應器來判斷用戶當前的屏幕方向,利用系統(tǒng)橫豎屏通知獲取當前橫豎屏狀態(tài);如果在重力感應獲取到用戶是橫屏狀態(tài)下,但是系統(tǒng)沒有通知過來,那就提醒用戶開啟橫屏鎖,否則就正常操作。
代碼實現(xiàn):
- 引入頭文件、初始化等
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
{
BOOL canRotate;//標志位:判斷現(xiàn)在能不能旋轉屏幕
}
@property (nonatomic, strong) CMMotionManager * motionManager;
@end
- (void)viewDidLoad {
[super viewDidLoad];
canRotate = NO;//這個參數(shù)是整個問題解決的核心,注意它的變化
[self startMotionManager];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil
];
}
- 系統(tǒng)通知部分過來的操作
//這個是系統(tǒng)橫豎屏通知過來,自己需要操作的方法
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
//1.獲取 當前設備 實例
UIDevice *device = [UIDevice currentDevice] ;
/**
* 2.取得當前Device的方向,Device的方向類型為Integer
*
* 必須調用beginGeneratingDeviceOrientationNotifications方法后,此orientation屬性才有效,否則一直是0。orientation用于判斷設備的朝向,與應用UI方向無關
*
* @param device.orientation
*
*/
switch (device.orientation) {
//其他情況屏幕方向就不一一列舉出來了
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左橫置");
canRotate = YES;//只有當用戶把手機旋轉到橫屏的時候來去觸發(fā)判斷是否支持橫屏,如果不支持就提醒用戶
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
canRotate = YES;
break;
default:
break;
}
}
- CMMotionManager部分代碼(如果不知道干啥的,自行百度)
//初始化
- (void)startMotionManager{
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc] init];
}
_motionManager.deviceMotionUpdateInterval = 1/15.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){
// UIDeviceOrientationPortraitUpsideDown;
}
else{
// UIDeviceOrientationPortrait;
}
}
else
{
if(canRotate == NO)
{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提醒" message:@"您關閉了橫屏鎖,請在控制中心打開" preferredStyle:UIAlertControllerStyleAlert];
[ac addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:ac animated:YES completion:nil];
}
if (x >= 0){
// UIDeviceOrientationLandscapeRight;
}
else{
// UIDeviceOrientationLandscapeLeft;
}
}
}
- 用完別忘記關掉
-(void)viewWillDisappear:(BOOL)animated
{
[_motionManager stopDeviceMotionUpdates];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
結束
如果有異議或者更好的方式歡迎留言!