iOS 中的界面旋轉

級別: ★☆☆☆☆
標籤:「iOS」「界面旋轉 」「iOS 中的界面旋轉」
做者: dac_1033
審校: QiShare團隊
php


最近所接觸的項目中有幾處視頻播放的需求,在該項目中視頻播放器能夠全屏/豎屏手動切換,也能夠自動切換,可是其餘界面都是豎屏狀態來展現。所以,總結了一下iOS中關於界面旋轉,即橫屏/豎屏切換相關的一些知識點。git

注意:在iOS中沒有顯式的設置界面方向的方法。github

1. 視圖view的旋轉

若是需求是隻對一個view進行旋轉,咱們能夠直接調用如下方法,對視圖進行手動旋轉。bash

view.transform = CGAffineTransformMakeRotation(M_PI_2);
複製代碼

若是你想用此方式實現把app當前的整個界面都旋轉成橫屏,你可能還須要如下方法:微信

// 狀態欄也要旋轉
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];

// 強制設置設備方向,以設置鍵盤彈出方向(注:但這是個私有方法)
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
複製代碼

2. 用系統方法實現界面旋轉

用系統方法實現的是界面的自動旋轉,首先,須要在工程中勾選一下選項: app

image.png

其次,在AppDelegate中實現如下方法:ide

// 整個app容許的自動旋轉的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
   
    return UIInterfaceOrientationMaskPortrait;
}
複製代碼

最後,在相應的ViewController中實現一下系統方法:佈局

// 是否可以旋轉
- (BOOL)shouldAutorotate;

// 支持的旋轉方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations;

// 模態視圖初始化時的旋轉方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
複製代碼

屏幕旋轉是從全屏視圖的方法來斷定的,即root視圖來斷定的。也就是說每次斷定頁面是否須要旋轉都是從圖中最左側的nav斷定。咱們要作到每一個頁面自定義的話須要在UINavigationController和UITabbarController中分別實現以下方法:ui

//// 在NavgationController的基類中粘貼如下代碼
- (BOOL)shouldAutorotate {

    return self.topViewController.shouldAutorotate;
}
 
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    return self.topViewController.supportedInterfaceOrientations;
}
 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return [self.topViewController preferredInterfaceOrientationForPresentation];
}


////  在TabbarController的基類中粘貼如下代碼
- (BOOL)shouldAutorotate {

    return self.selectedViewController.shouldAutorotate;
}
 
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    return self.selectedViewController.supportedInterfaceOrientations;
}
 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}
複製代碼

3. 獲取、監聽設備的方向

  • 獲取設備方向
// 獲取設備方向
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
複製代碼
  • 添加監聽設備旋轉的通知
// 添加監聽設備旋轉的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

- (void)deviceOrientationChange:(NSNotification *)notification {
    
    UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
    
    switch (orient) {
        case UIDeviceOrientationPortrait:
            
            break;
        case UIDeviceOrientationLandscapeLeft:
            
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            
            break;
        case UIDeviceOrientationLandscapeRight:
            
            break;
            
        default:
            break;
    }
}


// 添加監聽界面旋轉的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusBarOrientationChange:(NSNotification *)notification {
    
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    if (orientation == UIInterfaceOrientationLandscapeRight) {
        
    }
    
    if (orientation ==UIInterfaceOrientationLandscapeLeft) {

    }
    
     if (orientation == UIInterfaceOrientationPortrait){
         
     }
    
    if (orientation == UIInterfaceOrientationPortraitUpsideDown){

    }
}
複製代碼

4. 界面旋轉的應用實例

如開頭所說,若是app中有QiTabBarController、UINavigationController、UIViewController等多層次的界面狀況下,咱們只想讓某個界面自動旋轉功能,怎麼實現呢?
(1)基類UITabBarController 、QiNavigationController中實現上面👆的代碼;
(2)基類QiViewController中的shouldAutorotate方法返回NO;
(3)繼承於QiViewController的子controller中的shouldAutorotate方法返回YES,便可實現上述功能。
spa

5. 強制某個界面旋轉

在iOS3之前能夠經過UIDevice的setOrientation來實現,但以後變成了私有方法,不能直接調用。咱們利用 KVO機制去間接調用,是否可以提交併成功發佈至AppStore就靠運氣了!

//// 最直接的
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];


//// 含蓄的
- (void)forceOrientation {

    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 = UIInterfaceOrientationLandscapeLeft;//橫屏
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
複製代碼

工程源碼GitHub地址


小編微信:可加並拉入《QiShare技術交流羣》。

關注咱們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公衆號)

推薦文章:
iOS 經常使用佈局方式之Frame
iOS 經常使用佈局方式之Autoresizing
iOS 經常使用佈局方式之Constraint
iOS 經常使用佈局方式之StackView
iOS 經常使用佈局方式之Masonry
iOS UIButton根據內容自動佈局
iOS 指定初始化方法
UIView中的hitTest方法
奇舞週刊

相關文章
相關標籤/搜索