iOS 中的界面旋转

作者: QiShare | 来源:发表于2019-06-13 19:28 被阅读28次

级别: ★☆☆☆☆
标签:「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. 用系统方法实现界面旋转

用系统方法实现的是界面的自动旋转,首先,需要在工程中勾选一下选项:


image.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];
    }
}

工程源码GitHub地址


小编微信:可加并拉入《QiShare技术交流群》。

关注我们的途径有:
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方法
奇舞周刊

相关文章

  • iOS 中的界面旋转

    级别: ★☆☆☆☆标签:「iOS」「界面旋转 」「iOS 中的界面旋转」作者: dac_1033审校: QiSha...

  • iOS中的界面旋转

    最近所接触的项目中有几处视频播放的需求,在该项目中视频播放器可以全屏/竖屏手动切换,也可以自动切换,但是其他界面都...

  • Android仿酷狗旋转界面功能-自定义控件实现

    引言 该旋转界面主要是仿照IOS酷狗版的旋转界面来实现,本项目通过结合ValueAnimator、Rotation...

  • 【iOS】当设备方向已经旋转,转屏效果失效的解决方案

    一.问题分析 在开发中,视频界面有强转的需求。当iOS设备处于“禁止旋转”的状态下,强转都是正常的。当iOS设备处...

  • iOS 单个界面旋转屏幕

    1 创建UIDevice的category 2 AppDelegate里面设置屏幕旋转权限 3 目标界面ViewD...

  • iOS 响应者链

    概述 iOS 响应者链(Responder Chain) 是支持App界面交互的重要基础, 点击, 滑动, 旋转,...

  • iOS开发--屏幕旋转的问题

    首先,在iOS中展示界面的方式有两种,push和modal那么,在app中如果想要实现随心所欲的屏幕旋转,就要分两...

  • iOS 响应者链

    一、概述 iOS 响应者链(Responder Chain)是支撑 App 界面交互的重要基础,点击、滑动、旋转、...

  • IOS 屏幕旋转个人总结

    说下IOS屏幕旋转以及相关知识。涉及的知识点有:1、控制控制器方向。2、整套app控制器方向控制。3、屏幕旋转界面...

  • iOS强制单一界面 屏幕旋转

    iOS强制单一界面屏幕旋转大致两种方案吧, 1 一种是在Appdelegate中设置一个值 allowrotati...

网友评论

    本文标题:iOS 中的界面旋转

    本文链接:https://www.haomeiwen.com/subject/jtjefctx.html