iOS - 视频横竖屏切换的一些事

如何区分当下控制器是 视频控制器 还是 视图控制器

1、首先要知道系统方法是如何控制当前的控制器

//此方法可获取当前显示控制器
 supportedInterfaceOrientations

2、使用runtime中的交换方法
这里主要是替换系统的方法,这样就可以在系统方法中做一些操作,load方法的作用是在程序运行起来将会先加载load方法,加载完成后在加载各个类中的方法

+ (void)load
{
      Method supportedInterfaceOrientations = class_getInstanceMethod(self,@selector(supportedInterfaceOrientations));
      Method wt_supportedInterfaceOrientations = class_getInstanceMethod(self,@selector(wt_supportedInterfaceOrientations));
      method_exchangeImplementations(supportedInterfaceOrientations, wt_supportedInterfaceOrientations);
}

3、处理系统替换之后的方法做一些操作
判断一下,如果是视频控制器的话,就返回上、下、左、右。如果不是视频控制器的话,就返回只能竖屏。

if ([NSStringFromClass(vc.class) isEqualToString: @"ViewController"])
{
     return UIInterfaceOrientationMaskPortrait;
}
else
{
     return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

4、过滤掉TabBar,navi
这里注意一点通常项目中都会添加TabBar、navi,一般初学者在这里不清楚如何正确的获取当前显示的控制器,主要意思是过滤掉TabBar,navi

- (UIViewController *)getVc:(UIViewController *)vc
{
    if ([vc isKindOfClass: [UITabBarController class]])
    {
        UIViewController *tabBarVC = vc.childViewControllers.firstObject;
        return [self getVc: tabBarVC];
    }
    else if([vc isKindOfClass: [UINavigationController class]])
    {
        UIViewController *navVc = vc.childViewControllers.lastObject;
        return [self getVc: navVc];
    }
    else
    {
        return vc;
    }
}

代码如下:

#import "UIViewController+Extension.h"
#import <objc/message.h>
#import "ViewController.h"
@implementation UIViewController (Extension)

+ (void)load
{
    Method supportedInterfaceOrientations = class_getInstanceMethod(self, @selector(supportedInterfaceOrientations));
    
    Method wt_supportedInterfaceOrientations = class_getInstanceMethod(self, @selector(wt_supportedInterfaceOrientations));
    
    method_exchangeImplementations(supportedInterfaceOrientations, wt_supportedInterfaceOrientations);
}

// 要交换的方法。
- (UIInterfaceOrientationMask)wt_supportedInterfaceOrientations
{
   [self wt_supportedInterfaceOrientations];
    UIViewController *vc = [self getVc: self];
    if ([NSStringFromClass(vc.class) isEqualToString: @"ViewController"])
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
    }
}

// 获取当前显示的控制器,主要过滤TabBar,navi
- (UIViewController *)getVc:(UIViewController *)vc
{
    if ([vc isKindOfClass: [UITabBarController class]])
    {
        UIViewController *tabBarVC = vc.childViewControllers.firstObject;
        return [self getVc: tabBarVC];
    }
    else if([vc isKindOfClass: [UINavigationController class]])
    {
        UIViewController *navVc = vc.childViewControllers.lastObject;
        return [self getVc: navVc];
    }
    else
    {
        return vc;
    }
}
@interface UIViewController (Extension)


- (UIInterfaceOrientationMask)wt_supportedInterfaceOrientations;
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,726评论 1 14
  • 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标...
    VincentHK阅读 5,575评论 3 44
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,200评论 1 6
  • 一直在做一个梦,在很多山之间迷路了,眼看天色已经晚,想找一个住处或者找人问路,却找不到,心里充满迷茫和焦虑。 这也...
    懂会阅读 269评论 0 0
  • Gitlab简介 GitLab是一个Git的代码托管工具,有免费的社区版允许我们在本地搭建代码托管网站,也有付费的...
    乐百川阅读 16,225评论 8 20

友情链接更多精彩内容