iOS屏幕旋轉(zhuǎn)(橫豎屏)

一、屏幕旋轉(zhuǎn)方向監(jiān)聽(tīng)

1、UIDeviceOrientation:設(shè)備方向

iOS 定義了七種設(shè)備方向:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,                 // 未知方向,可能是設(shè)備(屏幕)斜置
    UIDeviceOrientationPortrait,                // 設(shè)備(屏幕)直立
    UIDeviceOrientationPortraitUpsideDown,      // 設(shè)備(屏幕)直立,上下顛倒
    UIDeviceOrientationLandscapeLeft,           // 設(shè)備(屏幕)向左橫置
    UIDeviceOrientationLandscapeRight,          // 設(shè)備(屏幕)向右橫置
    UIDeviceOrientationFaceUp,                  // 設(shè)備(屏幕)朝上平躺
    UIDeviceOrientationFaceDown                 // 設(shè)備(屏幕)朝下平躺
};
//注意:UIDeviceOrientation參考home鍵方向,如:home方向在右,設(shè)備(屏幕)方向向左(UIDeviceOrientationLandscapeLeft)

當(dāng)設(shè)備方向變化時(shí)候,發(fā)出UIDeviceOrientationDidChangeNotification通知;注冊(cè)監(jiān)聽(tīng)該通知,可以針不同的設(shè)備方向處理視圖展示。
注:手機(jī)鎖定豎屏后,UIDeviceOrientationDidChangeNotification通知就失效了。
代碼如下:

//開(kāi)啟和監(jiān)聽(tīng) 設(shè)備旋轉(zhuǎn)的通知(不開(kāi)啟的話,設(shè)備方向一直是UIInterfaceOrientationUnknown)
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:) 
                                     name:UIDeviceOrientationDidChangeNotification object:nil];
//設(shè)備方向改變的處理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (ddeviceOrientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左橫置");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;
        default:
            NSLog(@"無(wú)法辨識(shí)");
            break;
    }
}

//最后在dealloc中移除通知 和結(jié)束設(shè)備旋轉(zhuǎn)的通知
- (void)dealloc{
    //...
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
2、UIInterfaceOrientation:界面方向

iOS 定義了五種界面方向

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,       //未知方向
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,               //界面直立
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  //界面直立,上下顛倒
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,   //界面朝左
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft    //界面朝右
} __TVOS_PROHIBITED;

注:界面方向和設(shè)別方向有對(duì)應(yīng)關(guān)系,如界面的豎直方向就是 設(shè)備的豎直方向:UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown

要想界面旋轉(zhuǎn),
在如圖所示選擇支持的界面旋轉(zhuǎn)方向:

3321499760416_.pic_hd.jpg

同時(shí)在UIViewController中加入以下兩個(gè)方法:

///是否支持自動(dòng)轉(zhuǎn)屏
-(BOOL)shouldAutorotate
{
    return YES;///很明顯如果返回YES就可以,返回NO就不行
}
///如果上面方法返回YES則會(huì)根據(jù)這個(gè)方法判斷支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;///這個(gè)是所有方向
}

當(dāng)界面方向變化時(shí)候,先后發(fā)出 UIApplicationWillChangeStatusBarOrientationNotificationUIApplicationDidChangeStatusBarOrientationNotification通知,手機(jī)鎖定豎屏后,這兩個(gè)通知也失效了;注冊(cè)監(jiān)聽(tīng)這兩個(gè)通知,可以針對(duì)不同的界面方向處理視圖展示。
代碼如下所示:

//以監(jiān)聽(tīng)UIApplicationDidChangeStatusBarOrientationNotification通知為例
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleStatusBarOrientationChange:) 
                                     name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
//界面方向改變的處理
- (void)handleStatusBarOrientationChange: (NSNotification *)notification{
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOrientation) {
        case UIInterfaceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIInterfaceOrientationPortrait:
            NSLog(@"界面直立");
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"界面直立,上下顛倒");
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"界面朝左");
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"界面朝右");
            break;
        default:
            break;
    }
}
//最后在dealloc中移除通知
- (void)dealloc{
    //...
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}

3、UIInterfaceOrientationMask
UIInterfaceOrientationMask是為了集成多種UIInterfaceOrientation而定義的類型,和UIViewController相關(guān),一共有7種

iOS中的UIInterfaceOrientationMask定義

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

ViewController可以重寫- (UIInterfaceOrientationMask)supportedInterfaceOrientations方法返回類型,來(lái)決定UIViewController可以支持哪些界面方向。

//支持界面直立
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

二、視圖控制器中旋轉(zhuǎn)方向的設(shè)置

1、禁止橫屏操作

  • 方法一:在項(xiàng)目的General-->Deployment Info-->Device Orientation中,只勾選Portrait(豎屏)
  • 方法二:Device Orientation默認(rèn)設(shè)置,在Appdelegate中實(shí)現(xiàn)supportedInterfaceOrientationsForWindow:只返回UIInterfaceOrientationMaskPortraitt(豎屏)
-  (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {  
     return UIInterfaceOrientationMaskPortrait;  
}

參考鏈接:http://www.cocoachina.com/ios/20170711/19808.html

項(xiàng)目Demo托管在[GitHub][]上
[GitHub]:https://github.com/lizishiye/DeviceOrientation_Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 現(xiàn)在大部分的智能移動(dòng)設(shè)備通過(guò)自動(dòng)旋轉(zhuǎn),能夠自動(dòng)切換去呈現(xiàn)最適合當(dāng)前屏幕顯示的內(nèi)容,無(wú)疑大大提升了使用者的用戶...
    BladeWayne閱讀 10,275評(píng)論 2 54
  • [這是第11篇] 導(dǎo)語(yǔ): iOS App中大多數(shù)頁(yè)面是只展示豎屏下的效果,但是少部分頁(yè)面需要支持橫豎屏。本文分別介...
    南華coder閱讀 14,619評(píng)論 18 93
  • 01. 其實(shí),大家看過(guò)的雞湯也很多,類似于作者提出一個(gè)感人故事,幾經(jīng)波折,最后屌絲逆襲,追到白富美,走上人生巔峰。...
    dhrbdjkal閱讀 581評(píng)論 2 10
  • 很多人不理解為什么生活壓力那么大,許多人還舍不得離開(kāi)北京?大城市有一個(gè)很吸引人的東西:規(guī)則。 規(guī)則讓生活更簡(jiǎn)單。 ...
    李小墅閱讀 674評(píng)論 0 1
  • 一位營(yíng)銷前輩說(shuō),對(duì)客戶要有父母心,要像對(duì)待孩子一樣對(duì)待客戶。 我心想,這可真夠搞笑的,這個(gè)行當(dāng)大部分人,都像對(duì)待爺...
    麥子程閱讀 1,249評(píng)論 1 3

友情鏈接更多精彩內(nèi)容