美文网首页otherSiOS学习笔记iOS Developer
iOS旋转横屏(解决禁用旋转时全频横屏方法)

iOS旋转横屏(解决禁用旋转时全频横屏方法)

作者: 幻想无极 | 来源:发表于2016-08-24 17:43 被阅读1198次
    禁用设备自动旋转时的横屏方法

    监听设备旋转

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    //横屏方法
    - (void)orientChangeWithVideoSuperView:(UIView *)superView playerView:(UIView *)view playerViewRect:(CGRect)rect{
    
         UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
        self.player1.backgroundColor = [UIColor redColor];
        superView.layer.transform = CATransform3DIdentity;
        switch (orient)
        {
            case UIDeviceOrientationPortrait:
                superView.frame = [UIScreen mainScreen].bounds;
                view.frame = rect;
                break;
            default:
                superView.frame = CGRectMake(-CGRectGetHeight([UIScreen mainScreen].bounds)+CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
                view.frame = CGRectMake(0, 0,CGRectGetHeight([UIScreen mainScreen].bounds),CGRectGetWidth([UIScreen mainScreen].bounds));
                superView.transform = CGAffineTransformMakeRotation(M_PI_2);
                
                break;
        }
    }
    //通知设备旋转了
    - (void)orientChange:(NSNotification *)noti
    {
      
        [self orientChangeWithVideoSuperView:self.view playerView:self.player1 playerViewRect:CGRectMake(0, 0, 300, 200)];
    }
    

    原理:给要横屏的视图添加父视图,旋转这个父视图

    不禁用设备自动旋转横屏方法

    1.注意这种方式监听的是StatusBar也就是状态栏的方向,所以这个是跟你的布局有关的,你的布局转了,才会接到这个通知,而不是设备旋转的通知。

          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    - (void)statusBarOrientationChange:(NSNotification *)notification
    {
        
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右
        {
            //
        }
        
        if (
            orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
        {
            //
        }
        
        if (orientation == UIInterfaceOrientationPortrait)
        {
            //
        }
    
        if (orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            //
        }
    }
    

    2.注意到这种方式里面的方向还包括朝上或者朝下,很容易看出这个完全是根据设备自身的物理方向得来的,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:)name:UIDeviceOrientationDidChangeNotification object:nil];
    
    - (void)orientChange:(NSNotification *)noti
    {
        
        NSDictionary* ntfDict = [noti userInfo];
        
        UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
        /*
         UIDeviceOrientationUnknown,
         UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
         UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
         UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
         UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
         UIDeviceOrientationFaceUp,              // Device oriented flat, face up
         UIDeviceOrientationFaceDown             // Device oriented flat, face down   */
        
               switch (orient)
            {
                case UIDeviceOrientationPortrait:
                    
                    break;
                case UIDeviceOrientationLandscapeLeft:
        
                    
                    break;
                case UIDeviceOrientationPortraitUpsideDown:
     
              
                    break;
                case UIDeviceOrientationLandscapeRight:
            
               
                    break;
                    
                default:
                    break;
            }
    }
    
    禁用设备自动旋转

    1.禁用单个控制器

    - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED {
        return NO;
    }
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED {
        return UIInterfaceOrientationMaskAll;
    }
    // Returns interface orientation masks.
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED {
        return UIInterfaceOrientationPortrait;
    }
    
    

    2.禁用所有


    E03F9EAA-4E2B-454F-B088-1B4AF0441C7C.png
    补充

    找到一个更好的方法,直接强制横屏就行了

    需要在AppDelegate中添加属性

    @property (nonatomic,assign)BOOL isVerticalScreen;//竖屏
    

    AppDelegate.m中添加

    /**强制横屏*/
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (self.isVerticalScreen) {
            return UIInterfaceOrientationMaskPortrait;
        }else {
            return UIInterfaceOrientationMaskLandscapeLeft;
        }
        
    }
    

    相关文章

      网友评论

      本文标题:iOS旋转横屏(解决禁用旋转时全频横屏方法)

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