iOS实现屏幕旋转有两种方式

1. 应用本身支持

2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘、导航、UIAlertView)

 

如果你只是要把竖屏的播放器,做成支持横屏的,没有其他界面操作, 就可以考虑用第二种方式去做,比较简单 ,不过要注意计算view Frame

这两种方式看你具体的使用场景了,具体场景选择合适的方式。

公司项目中有几个界面要支持横竖屏,(直播录制界面、直播观看界面、视频回看界面)。 刚开始我想着用第二种方式去解决,但是我们视频录制、观看界面有输入框,需要调用键盘,没找到可以手动旋转键盘的方式,就改为让应用本身支持屏幕旋转。具体做法如下:

我们项目有一个 UITabBarController ,它有3个子CustomNavigationController, 我写了一个 

CustomNavigationController继承自 UINavigationController 

1. tabBarController 中 

- (BOOL)shouldAutorotate{

    return [self.selectedViewController shouldAutorotate];

}

//支持的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

    return [self.selectedViewController supportedInterfaceOrientations];

}

//初始方向

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

    return UIInterfaceOrientationPortrait;

}

2. CustomNavigationController 重写父类方法

- (BOOL)shouldAutorotate

{

    return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return UIInterfaceOrientationPortrait;

}

3. 我的支持屏幕旋转的3个ViewController是presentViewController出来的; 在支持旋转的界面加入以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown;

}

- (BOOL)shouldAutorotate

{

    if (可以在此处做判断,如果满足条件就可以旋转) {

        return YES;

    }

    return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    if (可以在此处做判断,如果满足条件就支持竖屏) {

        return UIInterfaceOrientationMaskPortrait;

    }

    return UIInterfaceOrientationMaskAllButUpsideDown;

}

还要做一件事---- 刷新UI布局;

4.

在ViewDidLoad中加入监听屏幕旋转的事件:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

在orientChange:方法中修改UI布局  ;

(注意: 看一下屏幕尺寸的变化 [[UIScreen mainScreen] bounds].size )

 

如果要控制CustomNavigationController push到的viewController的旋转,那么就在CustomNavigationController里面区分是哪个viewController,以便单独控制!

- (BOOL)shouldAutorotate

{

    UIViewController *control = self.topViewController;

    if ([control isKindOfClass:[@"支持旋转的ViewController" class]]) {

        return [control shouldAutorotate];

    }

    return YES;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    UIViewController *control = self.topViewController;

    if ([control isKindOfClass:[@"支持旋转的ViewController" class]]) {

        return [control supportedInterfaceOrientations];

    }

    return UIInterfaceOrientationMaskPortrait;

}

 

 

 

posted on 2016-03-08 15:03  爱拼  阅读(2252)  评论(0编辑  收藏  举报