美文网首页
iOS屏幕旋转

iOS屏幕旋转

作者: DDY | 来源:发表于2017-03-20 11:26 被阅读133次

方向分两种,一种是设备的方向,一种是视图方向。设备方向有两种方式可以改变,一个是通过重力加速计,即旋转屏幕的方式去改变,一个是通过代码
自然屏幕旋转就会有多种方式,比如:通过旋转window实现,通过旋转view实现,通过旋转layer实现,通过UIInterfaceOrientationMask实现。

一、直接设置 UIDevice 的 orientation(不推荐,可能被和谐)
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {  
    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];  
}
二、所有页面横屏(可使用 Interface Builder工具设计界面)
// 配置plist
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
三、调整window的方向,这样就不用每个view都改变了
// 重写方法四中方法为不允许自动
UIApplication *application=[UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
四、重写方法(支持重力感应)

TabbarController中

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

NavigationController中

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

某个控制器支持旋转(不支持则改变返回值)

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
五、设备旋转+视图旋转(最终我项目采用的方案)
#pragma mark - 控制旋转屏幕
#pragma mark 1.旋转设备
- (void)interfaceOrientation:(BOOL)rotate
{
    UIInterfaceOrientation orientation = rotate ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation];
    [[UIApplication sharedApplication] setStatusBarHidden:rotate];
}

#pragma mark 2.旋转视图
- (void)rotateView:(BOOL)rotate
{
    self.view.transform = CGAffineTransformMakeRotation(rotate ? M_PI_2 : 0);
    self.view.bounds = [UIScreen mainScreen].bounds;
}

#pragma mark 3.重新布局
- (void)reLayoutContentViews
{
    
}
是否允许旋转

Appdelagate.h中

@property (nonatomic,assign)NSInteger allowRotate;

Appdelegate.m中

#pragma mark - 控制旋转屏幕
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (_allowRotate == 1)
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        return (UIInterfaceOrientationMaskPortrait);
    }
}

需要旋转功能的控制器中

- (void)viewWillAppear:(BOOL)animated
{
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 1;
}

- (void)viewWillDisappear:(BOOL)animated
{
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;
// 防止pop到上一级界面仍然横屏
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
} 


iOS屏幕自动旋转问题 以及横屏模式打开APP出现的问题
iOS监听横竖屛通知

相关文章

  • 屏幕旋转

    屏幕旋转 推荐文档 了解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/hyubnttx.html