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. 用系统方法实现界面旋转

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

其次,在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方法
奇舞周刊

转载于:https://juejin.im/post/5d0233da6fb9a07eac05ce51

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值