iOS旋轉橫屏(解決禁用旋轉時全頻橫屏方法)

禁用設備自動旋轉時的橫屏方法

監聽設備旋轉

  [[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;
    }
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容