iOS设备旋转(整理)

作者: 景天儿 | 来源:发表于2016-08-22 16:38 被阅读130次

    旋转相关的配置方法

    //UIViewController.m
    #pragma mark - rotation
    //定义是否支持旋转
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    //定义支持旋转的方向
    -(UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        return isPad ? UIInterfaceOrientationMaskLandscape : UIInterfaceOrientationMaskPortrait;
    }
    //定义第一次进来的时候的方向
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return isPad ? UIInterfaceOrientationLandscapeLeft : UIInterfaceOrientationPortrait;
    }
    

    旋转相关的生命周期方法

    //UIViewController.m
    //1
    
    //被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
    
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    
    NSLog(@"willRotateToInterfaceOrientation %d",toInterfaceOrientation);
    
    }
    
    //2
    //window调整显示的view controller的bounds,由于view controller的bounds发生变化,将会触发 **viewWillLayoutSubviews** 方法。这个时候
    self.**interfaceOrientation**和**statusBarOrientation**方向还是原来的方向。
    
    //3
    
    //这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:
    
    - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
            //如果当前是竖屏要完成的事情
        }else {
            //其他情况完成事情
        }
    }
    
    //4
    
    //这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
    NSLog(@"didRotateFromInterfaceOrientation %d",fromInterfaceOrientation);
    
    }
    

    但是上面三个方法(1, 2, 4)在iOS8之后已经是Deprecated状态了

    // This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized). 
    // If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
    
    // This method is called when the view controller's trait collection is changed by its parent.
    // If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
    - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
    

    UIDeviceOrientation & UIInterfaceOrientation

    UIDeviceOrientation
    机器硬件方向,只读//可以反射设置
        [[UIDevice currentDevice] valueForKey:@"orientation"]
    UIInterfaceOrientation
    程序界面方向,读写。
        [[UIApplication sharedApplication] statusBarOrientation]
    

    appDelegate的application:supportedInterfaceOrientationsForWindow:

    当前controller能否旋转,取决于下面这个代理方法的返回值和controller的设置,这个值默认是plist里面UIInterfaceOrientation决定的,一般不需要重写这个方法。

    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    

    注意iOS7

    //iOS 7:
    //    竖屏:  
        UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)  
    //    横屏:  
        UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)  
    //iOS 8:
    //    竖屏:  
        UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)  
    //    横屏:  
        UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0) 
    

    还要注意弹框View与旋屏

    参考文献

    iOS屏幕旋转各类集锦(一) 设置
    iOS屏幕旋转各类集锦(二) 生命周期
    详解iOS开发中处理屏幕旋转的几种方法 (注意设备旋转和屏幕旋转的不同)

    相关文章

      网友评论

        本文标题:iOS设备旋转(整理)

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