开发了很久一直没有用到过屏幕旋转,一般app设置固定竖屏也就可以了,但是涉及到视频播放的时候全屏播放就是不得不实现的功能了,我们主要介绍两点
一、屏幕旋转监听
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//开始生成 设备旋转 通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//添加 设备旋转 通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
//销毁 设备旋转 通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil
];
//结束 设备旋转通知
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
/**屏幕旋转的通知回调*/
- (void)orientChange:(NSNotification *)noti {
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
switch (orient) {
case UIDeviceOrientationPortrait:
NSLog(@"竖直屏幕");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"手机左转");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"手机竖直");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"手机右转");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知");
break;
case UIDeviceOrientationFaceUp:
NSLog(@"手机屏幕朝上");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"手机屏幕朝下");
break;
default:
break;
}
}
备注已经很详细了
- UIDeviceOrientationUnknown 未知,屏幕旋转锁定时可能出现
- UIDeviceOrientationPortrait 手机垂直,home键在下边
- UIDeviceOrientationPortraitUpsideDown手机垂直,home键在上边
- UIDeviceOrientationLandscapeLeft 手机水平,home键在右边
- UIDeviceOrientationLandscapeRight 手机水平,home键在左边
- UIDeviceOrientationFaceUp 手机躺着
- UIDeviceOrientationFaceDown 手机趴着
还可以根据statusBar的方向判断
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationUnknown:
NSLog(@"未知");
break;
case UIInterfaceOrientationPortrait:
NSLog(@"竖直");
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"屏幕倒立");
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"手机水平,home键在左边");
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"手机水平,home键在右边");
break;
default:
break;
}
二、手动控制屏幕方向
如果我们想在页面中通过按钮设置屏幕方向,那么我们需要先设置支持的方向
//设置竖屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
//设置横屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
有人可能使用过以下方法,不过应该被禁了,不能上架
//setOrientation 在3.0以后变为私有方法了,不能直接去调用此方法,否则后果就是被打回。
在网上搜了很多很久,都是这种调用私有方法的:
//强制横屏,会被打回。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
三、要实现其他页面都是竖屏,到某一个页面变为横屏
1.创建子类CusNavigationController继承UINavigationController并写入如下方法
//支持旋转
-(BOOL)shouldAutorotate{
return [self.topViewController shouldAutorotate];
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
2.创建子类BaseViewController继承UIViewController并写入如下方法
//支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向 因为界面A我们只需要支持竖屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
3.控制器继承自BaseViewController并实现以下方法
//支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向 因为界面A我们只需要支持竖屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft;
}
四、如果Masonry涉及到NavigationBar的显示刷新问题可以😂
[self.navigationController.navigationBar.superview layoutSubviews];
⚠️警告 同时使用UIDeviceOrientationDidChangeNotification监听屏幕旋转方向并手动设置屏幕物理方向会出现死循环
参考文章
https://my.oschina.net/wolx/blog/387315
https://www.jianshu.com/p/37ea49830d11
https://www.cnblogs.com/niit-soft-518/p/5611298.html
https://blog.csdn.net/goods_boy/article/details/72631424