iOS屏幕旋转

iOS屏幕旋转

1.基本属性和概念

  • shouldAutorotate </br>
Returns a Boolean value indicating whether the view controller'��s contents should auto rotate.

控制器的内容是否自动旋转
  • supportedInterfaceOrientations </br>
Returns all of the interface orientations that the view controller supports.

控制器支持的选装方向 

注意:
UIDeviceOrientation    设备方向
UIInterfaceOrientation 屏幕视图方向

竖屏时设备方向和屏幕方向是一致的
横屏时设备方向和屏幕方向相反,如手机右转(home键在右侧)时,屏幕方向是左转的。

有部分三方没有及时升级 屏幕旋转后 View显示反了

  • preferredInterfaceOrientationForPresentation
Returns the interface orientation to use when presenting the view controller.

present View Controller 优先显示的方向

根控制器控制视图的是否支持自动旋转和旋转方向

即根控制器是UINavigationController

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

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

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

2、场景: 项目一直竖屏,现在有一个界面想横屏

第一种Push的方式

第一步:Device Orientaion中勾选Landscape Left 和 Landscape Right

第二步:基类VC自动旋转和支持方向的方法

//根视图默认不支持自动旋转
- (BOOL)shouldAutorotate
{
    return NO;
}

// 支持竖屏显示方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

第三步:在目标VC界面

//目标视图支持自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}

// 支持竖屏显示方向 -> UIInterfaceOrientationMaskLandscape
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

第四步:目标VC界面添加

  //强制转屏
  NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
  [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

//转屏后回调
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0)
{

    // NSLog(@"%d",[NSThread isMainThread]);
    // NSLog(@"%@",NSStringFromCGSize(size));
    // 记录当前是横屏还是竖屏
    
    // 翻转的时间
    CGFloat duration = [coordinator transitionDuration];
    [UIView animateWithDuration:duration animations:^{
        //转屏后刷新UI坐标
        [self reloadLandscapeView:size];
    }];
}

第二种present的方式

第一步:同上

第二步:同上 基类VC多添加一个方法

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

第三步:同上 目标VC多添加一个方法

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

第四步:目标VC界面重写方法

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
{
    
    [super willTransitionToTraitCollection:newCollection
                 withTransitionCoordinator:coordinator];
    
    [self reloadLandscapeView:CGSizeMake(MAX(self.view.frame.size.width, self.view.frame.size.height), MIN(self.view.frame.size.width, self.view.frame.size.height))];
}

Demo地址: https://github.com/lf-sytc/Project

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.导航控制器栈内部的VC方向是导航控制器来决定的。nav --- A --- B --- C,C的旋转方法是不起...
    FindCrt阅读 7,344评论 0 10
  • 1、UIDeviceOrientation 设备的物理方向 简介UIDeviceOrientation即我们手持的...
    MrJ的杂货铺阅读 27,954评论 8 75
  • 本文屏幕旋转方案仅限于兼容iOS8+ 1. 从APP层次谈起 APP常见的布局层次如下图所示: 里层UIViewC...
    AliThink阅读 4,680评论 0 10
  • 前言 现在大部分的智能移动设备通过自动旋转,能够自动切换去呈现最适合当前屏幕显示的内容,无疑大大提升了使用者的用户...
    BladeWayne阅读 13,387评论 2 54
  • 前言 这段时间公司由于有个需求,就是在App中有一个查看文件的页面,由于查看文件横屏会更方便阅读,所以boss说要...
    peaktan阅读 7,114评论 8 10