iOS屏幕旋转

作者: karas | 来源:发表于2017-05-09 14:28 被阅读573次

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

相关文章

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • OC - 屏幕旋转(自动旋转、手动旋转、兼容iOS6之前系统)

    导读: 一、iOS6之前屏幕旋转知识了解 二、iOS6(包括iOS6)之后屏幕旋转知识了解 三、自动旋转具体操作 ...

  • 屏幕旋转和弹出框

    iOS中控制屏幕旋转相关方法 shouldAutorotate:是否支持屏幕旋转 alertView:clicke...

  • iOS Rotation

    iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...

  • iOS传感器:实现一个随屏幕旋转的图片

    iOS传感器:实现一个随屏幕旋转的图片 iOS传感器:实现一个随屏幕旋转的图片

  • iOS 屏幕旋转处理

    iOS 屏幕旋转处理 在 iOS 设备上, 屏幕旋转是个挺坑比的问题. 苹果希望 app 对整个 app 做统一的...

  • iOS屏幕旋转

    方式一 假旋转 修改view的transform,通过旋转来实现横屏 工程配置 重写shouldAutorotat...

  • iOS 屏幕旋转

    第一种 在基类控制器,RootTableViewController,RootNaviViewController...

  • iOS屏幕旋转

    一、两种方向的区别 设备方向设备的物理方向,由类型UIDeviceOrientation表示,当前设备方向获取方式...

  • iOS屏幕旋转

    iOS屏幕旋转 1.基本属性和概念 shouldAutorotate supportedInterfaceOrie...

网友评论

    本文标题:iOS屏幕旋转

    本文链接:https://www.haomeiwen.com/subject/srobtxtx.html