開發(fā)了很久一直沒(méi)有用到過(guò)屏幕旋轉(zhuǎn)絮供,一般app設(shè)置固定豎屏也就可以了墩新,但是涉及到視頻播放的時(shí)候全屏播放就是不得不實(shí)現(xiàn)的功能了倍谜,我們主要介紹兩點(diǎn)
一崩泡、屏幕旋轉(zhuǎn)監(jiān)聽
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//開始生成 設(shè)備旋轉(zhuǎn) 通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//添加 設(shè)備旋轉(zhuǎn) 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//銷毀 設(shè)備旋轉(zhuǎn) 通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil
];
//結(jié)束 設(shè)備旋轉(zhuǎn)通知
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
/**屏幕旋轉(zhuǎn)的通知回調(diào)*/
- (void)orientChange:(NSNotification *)noti {
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
switch (orient) {
case UIDeviceOrientationPortrait:
NSLog(@"豎直屏幕");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"手機(jī)左轉(zhuǎn)");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"手機(jī)豎直");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"手機(jī)右轉(zhuǎn)");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知");
break;
case UIDeviceOrientationFaceUp:
NSLog(@"手機(jī)屏幕朝上");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"手機(jī)屏幕朝下");
break;
default:
break;
}
}
備注已經(jīng)很詳細(xì)了
- UIDeviceOrientationUnknown 未知禁荒,屏幕旋轉(zhuǎn)鎖定時(shí)可能出現(xiàn)
- UIDeviceOrientationPortrait 手機(jī)垂直,home鍵在下邊
- UIDeviceOrientationPortraitUpsideDown手機(jī)垂直角撞,home鍵在上邊
- UIDeviceOrientationLandscapeLeft 手機(jī)水平呛伴,home鍵在右邊
- UIDeviceOrientationLandscapeRight 手機(jī)水平,home鍵在左邊
- UIDeviceOrientationFaceUp 手機(jī)躺著
- UIDeviceOrientationFaceDown 手機(jī)趴著
還可以根據(jù)statusBar的方向判斷
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationUnknown:
NSLog(@"未知");
break;
case UIInterfaceOrientationPortrait:
NSLog(@"豎直");
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"屏幕倒立");
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"手機(jī)水平谒所,home鍵在左邊");
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"手機(jī)水平热康,home鍵在右邊");
break;
default:
break;
}
二睦焕、手動(dòng)控制屏幕方向
如果我們想在頁(yè)面中通過(guò)按鈕設(shè)置屏幕方向汇在,那么我們需要先設(shè)置支持的方向
設(shè)置支持的方向
//設(shè)置豎屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//設(shè)置橫屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
有人可能使用過(guò)以下方法,不過(guò)應(yīng)該被禁了宇攻,不能上架
//setOrientation 在3.0以后變?yōu)樗接蟹椒思馓裕荒苤苯尤フ{(diào)用此方法奕锌,否則后果就是被打回。
在網(wǎng)上搜了很多很久村生,都是這種調(diào)用私有方法的:
//強(qiáng)制橫屏惊暴,會(huì)被打回。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
三梆造、要實(shí)現(xiàn)其他頁(yè)面都是豎屏缴守,到某一個(gè)頁(yè)面變?yōu)闄M屏
1.創(chuàng)建子類CusNavigationController繼承UINavigationController并寫入如下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return [self.topViewController shouldAutorotate];
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
2.創(chuàng)建子類BaseViewController繼承UIViewController并寫入如下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向 因?yàn)榻缑鍭我們只需要支持豎屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
3.控制器繼承自BaseViewController并實(shí)現(xiàn)以下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向 因?yàn)榻缑鍭我們只需要支持豎屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft;
}
四葬毫、如果Masonry涉及到NavigationBar的顯示刷新問(wèn)題可以??
[self.navigationController.navigationBar.superview layoutSubviews];
??警告 同時(shí)使用UIDeviceOrientationDidChangeNotification監(jiān)聽屏幕旋轉(zhuǎn)方向并手動(dòng)設(shè)置屏幕物理方向會(huì)出現(xiàn)死循環(huán)
參考文章
https://my.oschina.net/wolx/blog/387315
http://www.reibang.com/p/37ea49830d11
https://www.cnblogs.com/niit-soft-518/p/5611298.html
https://blog.csdn.net/goods_boy/article/details/72631424