iOS開發(fā)之屏幕旋轉(zhuǎn)

開發(fā)了很久一直沒(méi)有用到過(guò)屏幕旋轉(zhuǎn)絮供,一般app設(shè)置固定豎屏也就可以了墩新,但是涉及到視頻播放的時(shí)候全屏播放就是不得不實(shí)現(xiàn)的功能了倍谜,我們主要介紹兩點(diǎn)

一崩泡、屏幕旋轉(zhuǎn)監(jiān)聽

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //開始生成 設(shè)備旋轉(zhuǎn) 通知
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    //添加 設(shè)備旋轉(zhuǎn) 通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}




-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //銷毀 設(shè)備旋轉(zhuǎn) 通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:nil
     ];
    
    
    //結(jié)束 設(shè)備旋轉(zhuǎn)通知
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
/**屏幕旋轉(zhuǎn)的通知回調(diào)*/
- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
    switch (orient) {
        case UIDeviceOrientationPortrait:
            NSLog(@"豎直屏幕");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"手機(jī)左轉(zhuǎn)");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"手機(jī)豎直");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"手機(jī)右轉(zhuǎn)");
            break;
        case UIDeviceOrientationUnknown:
            NSLog(@"未知");
            break;
        case UIDeviceOrientationFaceUp:
            NSLog(@"手機(jī)屏幕朝上");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"手機(jī)屏幕朝下");
            break;
        default:
            break;
    }
}

備注已經(jīng)很詳細(xì)了

  • UIDeviceOrientationUnknown 未知禁荒,屏幕旋轉(zhuǎn)鎖定時(shí)可能出現(xiàn)
  • UIDeviceOrientationPortrait 手機(jī)垂直,home鍵在下邊
  • UIDeviceOrientationPortraitUpsideDown手機(jī)垂直角撞,home鍵在上邊
  • UIDeviceOrientationLandscapeLeft 手機(jī)水平呛伴,home鍵在右邊
  • UIDeviceOrientationLandscapeRight 手機(jī)水平,home鍵在左邊
  • UIDeviceOrientationFaceUp 手機(jī)躺著
  • UIDeviceOrientationFaceDown 手機(jī)趴著

還可以根據(jù)statusBar的方向判斷

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        
        switch (orientation) {
            case UIInterfaceOrientationUnknown:
                NSLog(@"未知");
                break;
            case UIInterfaceOrientationPortrait:
                NSLog(@"豎直");
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                NSLog(@"屏幕倒立");
                break;
            case UIInterfaceOrientationLandscapeLeft:
                NSLog(@"手機(jī)水平谒所,home鍵在左邊");
                break;
            case UIInterfaceOrientationLandscapeRight:
                NSLog(@"手機(jī)水平热康,home鍵在右邊");
                break;
            default:
                break;
        }

二睦焕、手動(dòng)控制屏幕方向

如果我們想在頁(yè)面中通過(guò)按鈕設(shè)置屏幕方向汇在,那么我們需要先設(shè)置支持的方向

設(shè)置支持的方向
//設(shè)置豎屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];


//設(shè)置橫屏
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

有人可能使用過(guò)以下方法,不過(guò)應(yīng)該被禁了宇攻,不能上架

//setOrientation 在3.0以后變?yōu)樗接蟹椒思馓裕荒苤苯尤フ{(diào)用此方法奕锌,否則后果就是被打回。
在網(wǎng)上搜了很多很久村生,都是這種調(diào)用私有方法的:
//強(qiáng)制橫屏惊暴,會(huì)被打回。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}

三梆造、要實(shí)現(xiàn)其他頁(yè)面都是豎屏缴守,到某一個(gè)頁(yè)面變?yōu)闄M屏

1.創(chuàng)建子類CusNavigationController繼承UINavigationController并寫入如下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
    return [self.topViewController shouldAutorotate];
}

//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.topViewController supportedInterfaceOrientations];
}
2.創(chuàng)建子類BaseViewController繼承UIViewController并寫入如下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
    return YES;
}

//支持的方向 因?yàn)榻缑鍭我們只需要支持豎屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
3.控制器繼承自BaseViewController并實(shí)現(xiàn)以下方法
//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate{
    return YES;
}

//支持的方向 因?yàn)榻缑鍭我們只需要支持豎屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft;
}

四葬毫、如果Masonry涉及到NavigationBar的顯示刷新問(wèn)題可以??

[self.navigationController.navigationBar.superview layoutSubviews];

??警告 同時(shí)使用UIDeviceOrientationDidChangeNotification監(jiān)聽屏幕旋轉(zhuǎn)方向并手動(dòng)設(shè)置屏幕物理方向會(huì)出現(xiàn)死循環(huán)

參考文章
https://my.oschina.net/wolx/blog/387315
http://www.reibang.com/p/37ea49830d11
https://www.cnblogs.com/niit-soft-518/p/5611298.html
https://blog.csdn.net/goods_boy/article/details/72631424

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市屡穗,隨后出現(xiàn)的幾起案子贴捡,更是在濱河造成了極大的恐慌,老刑警劉巖村砂,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烂斋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡础废,警方通過(guò)查閱死者的電腦和手機(jī)汛骂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)评腺,“玉大人帘瞭,你說(shuō)我怎么就攤上這事≥锛ィ” “怎么了蝶念?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)芋绸。 經(jīng)常有香客問(wèn)我媒殉,道長(zhǎng),這世上最難降的妖魔是什么摔敛? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任廷蓉,我火速辦了婚禮,結(jié)果婚禮上马昙,老公的妹妹穿的比我還像新娘桃犬。我一直安慰自己,他們只是感情好行楞,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布疫萤。 她就那樣靜靜地躺著,像睡著了一般敢伸。 火紅的嫁衣襯著肌膚如雪扯饶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天池颈,我揣著相機(jī)與錄音尾序,去河邊找鬼。 笑死躯砰,一個(gè)胖子當(dāng)著我的面吹牛每币,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播琢歇,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼兰怠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼梦鉴!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起揭保,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤肥橙,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后秸侣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體存筏,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年味榛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了椭坚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡搏色,死狀恐怖善茎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情频轿,我是刑警寧澤巾表,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站略吨,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏考阱。R本人自食惡果不足惜翠忠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望乞榨。 院中可真熱鬧秽之,春花似錦、人聲如沸吃既。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鹦倚。三九已至河质,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間震叙,已是汗流浹背掀鹅。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留媒楼,地道東北人乐尊。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像划址,于是被迫代替她去往敵國(guó)和親扔嵌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子限府,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容