iOS 中的界面旋转
级别: ★☆☆☆☆
标签:「iOS」「界面旋转 」「iOS 中的界面旋转」
作者: dac_1033
审校: QiShare团队
最近所接触的项目中有几处视频播放的需求,在该项目中视频播放器可以全屏/竖屏手动切换,也可以自动切换,但是其他界面都是竖屏状态来展示。因此,总结了一下iOS中关于界面旋转,即横屏/竖屏切换相关的一些知识点。
注意:在iOS中没有显式的设置界面方向的方法。
1. 视图view的旋转
如果需求是只对一个view进行旋转,我们可以直接调用以下方法,对视图进行手动旋转。
view.transform = CGAffineTransformMakeRotation(M_PI_2);
如果你想用此方式实现把app当前的整个界面都旋转成横屏,你可能还需要以下方法:
// 状态栏也要旋转
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
// 强制设置设备方向,以设置键盘弹出方向(注:但这是个私有方法)
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
2. 用系统方法实现界面旋转
用系统方法实现的是界面的自动旋转,首先,需要在工程中勾选一下选项:
![](https://img.haomeiwen.com/i13180946/ef950f57f6035d0f.png)
其次,在AppDelegate中实现以下方法:
// 整个app允许的自动旋转的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskPortrait;
}
最后,在相应的ViewController中实现一下系统方法:
// 是否能够旋转
- (BOOL)shouldAutorotate;
// 支持的旋转方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations;
// 模态视图初始化时的旋转方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
屏幕旋转是从全屏视图的方法来判定的,即root视图来判定的。也就是说每次判定页面是否需要旋转都是从图中最左侧的nav判定。我们要做到每个页面自定义的话需要在UINavigationController和UITabbarController中分别实现如下方法:
//// 在NavgationController的基类中粘贴以下代码
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
//// 在TabbarController的基类中粘贴以下代码
- (BOOL)shouldAutorotate {
return self.selectedViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.selectedViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}
3. 获取、监听设备的方向
- 获取设备方向
// 获取设备方向
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
- 添加监听设备旋转的通知
// 添加监听设备旋转的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)deviceOrientationChange:(NSNotification *)notification {
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
switch (orient) {
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
}
}
// 添加监听界面旋转的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
- (void)statusBarOrientationChange:(NSNotification *)notification {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight) {
}
if (orientation ==UIInterfaceOrientationLandscapeLeft) {
}
if (orientation == UIInterfaceOrientationPortrait){
}
if (orientation == UIInterfaceOrientationPortraitUpsideDown){
}
}
4. 界面旋转的应用实例
如开头所说,如果app中有QiTabBarController、UINavigationController、UIViewController等多层次的界面情况下,我们只想让某个界面自动旋转功能,怎么实现呢?
(1)基类UITabBarController 、QiNavigationController中实现上面👆的代码;
(2)基类QiViewController中的shouldAutorotate方法返回NO;
(3)继承于QiViewController的子controller中的shouldAutorotate方法返回YES,即可实现上述功能。
5. 强制某个界面旋转
在iOS3以前可以通过UIDevice的setOrientation来实现,但之后变成了私有方法,不能直接调用。我们利用 KVO机制去间接调用,是否能够提交并成功发布至AppStore就靠运气了!
//// 最直接的
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
//// 含蓄的
- (void)forceOrientation {
if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeLeft;//横屏
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
小编微信:可加并拉入《QiShare技术交流群》。
![](https://img.haomeiwen.com/i1977357/fd12ad8916f18219.jpeg)
关注我们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)
推荐文章:
iOS 常用布局方式之Frame
iOS 常用布局方式之Autoresizing
iOS 常用布局方式之Constraint
iOS 常用布局方式之StackView
iOS 常用布局方式之Masonry
iOS UIButton根据内容自动布局
iOS 指定初始化方法
UIView中的hitTest方法
奇舞周刊