iOS 项目整体是竖屏,个别页面支持横屏

来源:互联网 发布:java 汽车租赁管理系统 编辑:程序博客网 时间:2024/06/09 18:16

        最近在做一个视频APP,工程整体是竖屏的,如下图,播放器需支持横屏。在转屏的过程遇到很痛苦的问题。[UIApplication sharedApplication].statusBarOrientation = self.UIInterfaceOrientationLandscapeLeft; 未生效设置完后,还是UIInterfaceOrientationPortrait。查阅资料是UITabbarController的方法- (BOOL)shouldAutorotate没有返回NO。这是因为当UIviewController方法- (BOOL)shouldAutorotate 返回值为YES的时候是不生效的


我遇到的问题如图:从横屏到竖屏有个停顿,然后再竖过来。并且statusbar也没转过来。


下面介绍一下如何做到“iOS 项目整体是竖屏,个别页面支持竖屏”,需要以下几步:

1.工程设置为Portrait,如第一张图

2.在appdelegate中实现方法:- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 如下:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    VideoPlayerViewController* videoPlayerVC = [VideoPlayerViewController defaultVideoPlayerViewController];    if ([VideoPlayerViewController isBeingShowed]        && VideoPlayerViewModeFullscreen == videoPlayerVC.viewMode        && !videoPlayerVC.presentedViewController        && !videoPlayerVC.isVideoPlayerDestroying)    {        return UIInterfaceOrientationMaskLandscape;/*在需要横屏的controller显示的时候返回横屏,其他页面返回竖屏*/    }    return UIInterfaceOrientationMaskPortrait;/*其他页面返回竖屏*/}

3. 现在APP普遍使用tab模式。所以还需要在UITabbarController和UINavigationController。这是因为需要横屏的UIViewController一半在UINavigationController上,而UINavigationController又在tabbar上,tabbar又在window上。这一系列都要实现才行。UITabbarController可以直接重写- (BOOL)shouldAutorotate,不在累赘
下面是UINavigationController的分类。

@implementation UINavigationController (Rotation)      - (BOOL)shouldAutorotate  {      return [[self.viewControllers lastObject] shouldAutorotate];  }      - (NSUInteger)supportedInterfaceOrientations  {      return [[self.viewControllers lastObject] supportedInterfaceOrientations];  }      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {      return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];  }  @end  

4. 在VideoPlayerViewController中实现- (BOOL)shouldAutorotate{},在需要横屏的情况返回yes,其他情况返回NO。方法和- (UIInterfaceOrientationMask)supportedInterfaceOrientations{},

5. 一般视频的转横屏都是新开一个window叫playerWindow。将视频的VideoPlayerViewController的view添加到playerWindow上。转竖屏时候再将playerWindow置位nil。VideoPlayerViewController.view再恢复到原始的window之上。简要代码如下:

/*转为竖屏时候需要回复原始window*/- (void)removeModelPlayerViewController {    [self removeModelPlayerNotification];    UIWindow* playerWindow = self.playerWindow;    if (playerWindow) {        VideoPlayerViewController* videoPlayerVC = self.videoPlayerViewController;                videoPlayerVC.view.transform = CGAffineTransformIdentity;        videoPlayerVC.view.frame = self.bounds;        videoPlayerVC.videoPlayerView.frame = self.bounds;        [self addSubview:videoPlayerVC.view];        [self.originWindow makeKeyAndVisible];        playerWindow.hidden = YES;        playerWindow.rootViewController = nil;        [playerWindow removeAllSubviews];        self.playerWindow = nil;        self.originWindow = nil;    }}/*横屏时候,开一个window,并makeKeyAndVisible*/- (void)registerPlayerWindow {    if (nil == self.playerWindow) {        self.originWindow = [[[UIApplication sharedApplication] delegate] window];        CGRect frame = [UIScreen mainScreen].bounds;        // 交换长宽,解决iOS8+window尺寸bug        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8") && frame.size.width < frame.size.height) {            frame.size.width = frame.size.width+frame.size.height-(frame.size.height = frame.size.width);        }        UIWindow* tmpWindow = [[UIWindow alloc] initWithFrame:frame];        self.playerWindow = tmpWindow;        tmpWindow.backgroundColor = [UIColor clearColor];        tmpWindow.windowLevel = UIWindowLevelNormal;                VideoPlayerViewController* videoPlayerVC = self.videoPlayerViewController;        tmpWindow.rootViewController = videoPlayerVC;        videoPlayerVC.view.frame = tmpWindow.bounds;        videoPlayerVC.view.backgroundColor = [UIColor clearColor];        [tmpWindow addSubview:videoPlayerVC.view];        [tmpWindow makeKeyAndVisible];                self.originOrientation = [[UIApplication sharedApplication] statusBarOrientation];    }}
出了上面这些,你还需要实现旋转动画。看起来才舒服一些。

刚开始,没有重写UITabbarController的- (BOOL)shouldAutorotate,遇到的坑,如图横屏时候playerWindow横过来了,但原始window也横过来了,当恢复横屏的时候,先是半屏最后才竖屏。


参考文章:http://blog.csdn.net/ginhoor/article/details/20454229

1 0
原创粉丝点击