zoukankan      html  css  js  c++  java
  • IOS开发——手动设置屏幕旋转

    在移动开发过程。您可能需要跨越看看你的手机。有可能是所有的接口必须跨越,有可能是一个交叉通过电话,当用户当,你的接口也希望他能跨越。还有可能的是,界面的一部分需要被侧向显示。视情况而定,有不同的方法来解决。

    首先,我们想澄清,以两种方式方向,向。一种是视图方向。设备方向有两种方式能够改变,一个是通过重力加速计,即旋转屏幕的方式去改变,一个是通过代码,调用UIDevice的方式去改变!

    直接设置 UIDevice 的 orientation,可是这样的方式不推荐,上传appStore有被拒的风险。

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {  
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];  
    }


    我们都是通过改变视图的方向来让屏幕旋转的。


    情景一:程序中全部界面都是横屏显示的

    解决的方法:改动project配置文件plist。里面的UISupportedInterfaceOrientations属性表示程序支持的方向。我们把它改成仅仅支持Left和Right

    <key>UISupportedInterfaceOrientations</key>
    <array>
    	<string>UIInterfaceOrientationLandscapeLeft</string>
    	<string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    UIInterfaceOrientationPortrait表示Home键button的方向,也就是竖屏方向,不要他

    这样。程序启动的时候就是横屏显示了,当然你须要使用横屏的插件来设计界面了,能够使用

     Interface Builder工具 

    解决的方法二:调整window的方向。这样就不用每一个view都改变了
    设置状态条的方向和使用transform旋转一下window.不翻状态条的方向,会出现键盘也不会跟着翻.假设仅仅是翻了某个view,那么会出现其他view不会翻.对于我的详细情况,翻window最简单.
    UIApplication *application=[UIApplication sharedApplication];
    [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
    注:须要设置程序不会自己主动响应自己主动旋转
    //由于想要手动旋转,所以先关闭自己主动旋转
    - (BOOL)shouldAutorotate{
        return NO;
    }

    情景二:程序中仅仅有某个view须要横屏显示,手动设置。不支持重力加速器感应
    解决的方法:在指定的ViewController里面进行设置,取消自己主动旋转,调用CGAffineTransformMakeRotation让视图旋转。并又一次定义大小
    这时plist里面就不能像之前那么设置了。要把UIInterfaceOrientationPortrait也加进去,由于还有别的页面是竖屏的
    首先关闭自己主动旋转,在ViewController.m里面加上这句
    //由于想要手动旋转,所以先关闭自己主动旋转
    - (BOOL)shouldAutorotate{
        return NO;
    }
    然后就是实现旋转的代码了,我们使用的是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        //实例化chart
        cv =[[ChartViewBase alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
        [self.view addSubview:cv];
        
        //旋转屏幕,可是仅仅旋转当前的View
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
        self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        self.view.bounds = CGRectMake(0, 0, frame.size.height, 320);
    }
    
    注意:
    1. 仅仅须要改变self.view.transform,那么self.view的全部subview都会跟着自己主动变;其次由于方向变了,所以self.view的大小须要又一次设置,不要使用self.view.frame,而是用bounds。
    2. 假设shouldAutorotate 返回YES的话,以下设置setStatusBarOrientation 是无论用的!setStatusBarOrientation仅仅有在shouldAutorotate 返回NO的情况下才管用。


    情景三:程序须要支持重力感应。当用户把手机横过来,程序也要横过来
    解决方式:又一次下面两个方法
    - (BOOL)shouldAutorotate;  
    - (NSUInteger)supportedInterfaceOrientations;

    IOS6里面,控制某个viewController旋转并非像IOS5或者IOS4一样在这个viewController里面重写上面那2个方法,而是须要在这个viewController的rootViewController(根视图控制器)里面重写,怎么解释呢?就是最前面的那个viewController,直接跟self.window接触的那个controller。

    UIViewController *viewCtrl = [[UIViewController alloc] init];  
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];  
    if ([window respondsToSelector:@selector(setRootViewController:)]) {  
        self.window.rootViewController = navCtrl;  
    } else {  
        [self.window addSubview:navCtrl.view];  
    } 

    假设须要设置viewCtrl的旋转。那么不能在UIViewController里面重写shouldAutorotate和supportedInterfaceOrientations方法,而是须要在navCtrl里面设置,又由于UINavigationController是系统控件,所以这里须要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比方:
    -(NSUInteger)supportedInterfaceOrientations{  
        return UIInterfaceOrientationMaskAllButUpsideDown;  
    }  
      
    - (BOOL)shouldAutorotate{  
        return YES;  
    }  


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Bean复制
    java中的ConcurrentModificationException异常
    线程安全问题
    多线程等待唤醒机制之生产消费者模式
    JavaScript数据结构——队列的实现
    JavaScript数据结构——链表的实现
    JavaScript数据结构——栈的实现
    java中map集合的迭代
    SQLServer查询最近一天,三天,一周,一月,一季度方法
    细数网络上十七种安全威胁
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4741234.html
Copyright © 2011-2022 走看看