IOS 不同根视图下控制部分屏幕旋转

一、几个核心的方法


1.1屏幕旋转方向

- (BOOL)shouldAutorotate//是否支持旋转屏幕{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations//支持哪些方向{
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation//默认显示的方向{
    return UIInterfaceOrientationPortrait;
}

方法调用有限制
官方原文:

image.png

1.也就是只有在window 的 rootcontroller 或者 window最上层prensented controller里面使用上面三个函数,用户旋转的时候才能得到调用。
2.只有shouldAutorotate 返回YES时其他两个函数才能得到调用,返回为NO的时候默认为UIInterfaceOrientationMaskPortrait方向。

因此要控制某个界面旋转只能在windows 的控制器里面,覆盖以上上个方法,并在supportedInterfaceOrientations 获取你需要旋转的控制器的方向。具体的使用代码下面有讨论根控制器分别为UITabBarControllerUINavigationController的情况。

1.2 代码切换屏幕方向

//代码切换屏幕方向
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

1.3 显示旋转横屏时消失的状态栏

info.plist文件中将 View controller-based status bar appearance 设置为NOapplication:didFinishLaunchingWithOptions:中添加下面代码

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

如此之后你就会发现消失的状态条又乖乖的回来了。

1.4 屏幕旋转的几个代理方法

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_DEPRECATED_IOS(2_0,8_0, "Implement viewWillTransitionToSize:withTransitionCoordinator: instead") __TVOS_PROHIBITED;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation NS_DEPRECATED_IOS(2_0,8_0) __TVOS_PROHIBITED;

方便控制器对屏幕旋转做出相应的响应:

image.png

1.5 监听设备的旋转

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
-(void)onDeviceOrientationChange  
{  
   
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;  
    if (UIDeviceOrientationIsLandscape(orientation)) {  
            [self.view setNeedsLayout];  
            [self.view layoutIfNeeded];  
    }else if (orientation==UIDeviceOrientationPortrait){  
           [self.collectionView reloadData];  
            [self.view setNeedsLayout];  
            [self.view layoutIfNeeded];  
   
    }  
}  

二、系统支持横屏顺序


(1) 默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选


861981-58be5bbebac5ecd1.jpeg

只要在plist或者General设置了单一方向,在工程里面的任何代码都会失效。所以想要旋转屏幕,必须让你的工程支持所旋转的方向。
(但是这里还有一种方式是把当前控制器的View或者view.layer做transform旋转,但是我认为这并不是真正的旋转屏幕,一点可以证明的就是:状态条并不随着旋转)
(2)application window设置的级别次之
(3)然后是UINavigationcontroller
(4)级别最低的是viewcontroller

三、设置部分控制器旋转


具体需求描述如下:
要求app 推入到TGLivePlayController时,(1)用户点击视频全屏/退出全屏书时,自定义播放view可以做出响应的全屏/16:9显示 (2)用户退出TGLivePlayController时,app自动切换到竖屏状态。

3.1 根控制器为UINavigationController

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIViewController *vc = self.visibleViewController;
    if([vc isKindOfClass:[TGLiveVideoController class]]){
        return [vc supportedInterfaceOrientations];
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

-(BOOL)shouldAutorotate{
    UIViewController *vc = self.visibleViewController;
    return [vc shouldAutorotate];
   
   //也可以这样写只在指定的vc里面响应,只是这样在退出指定的控制器时不能在willdisappear里面还原竖屏。
    if([vc isKindOfClass:[TGLiveVideoController class]]){
        return [vc supportedInterfaceOrientations];
    }
    return NO;
}

3.2 根视图控制器为TabBarController

创建NavigationController和TabBarVontroller的category
这里注意不能在tabbarcontroller的类里写旋转方法 否则以下方法会无效

UITabBarController+autoRotate.h
#import <UIKit/UIKit.h>  
  
@interface UITabBarController (autoRotate)  
-(BOOL)shouldAutorotate;  
-(NSUInteger)supportedInterfaceOrientations;  
@end  
UITabBarController+autoRotate.m
#import "UITabBarController+autoRotate.h"  
  
@implementation UITabBarController (autoRotate)  
-(BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end  
UINavigationController+autoRotate.h
@interface UINavigationController (autoRotate)  
-(BOOL)shouldAutorotate;  
-(NSUInteger)supportedInterfaceOrientations;  
@end  
UINavigationController+autoRotate.m
-(BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

3.3 控制器代码

TGLiveVideoController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self LVSetUpNavItem];
    [self LVSetUpSubviews];
}

-(void)LVSetUpNavItem{
    self.navigationItem.leftBarButtonItem = nil;//移除原有的统一定制的返回键
    UIButton *LeftItem = [UIButton new];
    LeftItem.frame = CGRectMake(0, 0, 24, 24);
    [LeftItem.imageView setContentMode:UIViewContentModeCenter];
    [LeftItem setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [LeftItem setBackgroundImage:[UIImage imageNamed:@"nav_fanhui"] forState:UIControlStateNormal];
    [LeftItem addTarget:self action:@selector(LVbackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customLeftItem = [[UIBarButtonItem alloc] initWithCustomView:LeftItem];
    self.navigationItem.leftBarButtonItem = customLeftItem;
}
#pragma mark - 替换统一生成的返回键事件
-(void)LVbackBtnClick{
    //先旋转后退回
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGFloat videoWidth = kScreenWidth;
    CGFloat videoHeight = _isFullScreen ? self.view.bounds.size.height : kScreenWidth / 16.0f * 9.0f;
    [self.player setPreviewFrame:CGRectMake(0, 0, videoWidth, videoHeight)];
    
    CGFloat btnW = 60.0f;
    CGFloat btnH = 40.0f;
    CGFloat btnX = CGRectGetMaxX(self.player.previewView.frame) - btnW;
    CGFloat btnY = CGRectGetMaxY(self.player.previewView.frame) - btnH;
    self.btnFullScreen.frame = CGRectMake(btnX, btnY, btnW, btnH);
}

-(BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

#pragma mark - 旋转代理事件
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration{
    _isFullScreen = NO;
    //横屏情况
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
       toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        _isFullScreen = YES;
    }
    self.btnFullScreen.selected = _isFullScreen;
}

#pragma mark - 全屏点击
-(void)btnFullScreenClick:(UIButton *)sender{
    BOOL state = !self.isFullScreen;
    if(state){
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }else{
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }

}

Rotation Issues in ios 6

参考文献:

iOS指定页面屏幕旋转,手动旋转(某app实现功能全过程)
不同根视图下控制部分屏幕旋转

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271