【轉(zhuǎn)】iOS開發(fā):屏幕的旋轉(zhuǎn)

Device Orientation:設(shè)備方向

typedefNS_ENUM(NSInteger,UIDeviceOrientation){UIDeviceOrientationUnknown,UIDeviceOrientationPortrait,// Device oriented vertically, home button on the bottomUIDeviceOrientationPortraitUpsideDown,// Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,// Device oriented horizontally, home button on the rightUIDeviceOrientationLandscapeRight,// Device oriented horizontally, home button on the leftUIDeviceOrientationFaceUp,// Device oriented flat, face upUIDeviceOrientationFaceDown// Device oriented flat, face down}

UIInterfaceOrientation:界面方向

typedefNS_ENUM(NSInteger,UIInterfaceOrientation){UIInterfaceOrientationUnknown=UIDeviceOrientationUnknown,UIInterfaceOrientationPortrait=UIDeviceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown=UIDeviceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft=UIDeviceOrientationLandscapeRight,UIInterfaceOrientationLandscapeRight=UIDeviceOrientationLandscapeLeft}

屏幕旋轉(zhuǎn)的兩個(gè)通知

// 硬件旋轉(zhuǎn)通知俊戳,此時(shí)界面還未完成旋轉(zhuǎn)可設(shè)置界面的旋轉(zhuǎn)UIDeviceOrientationDidChangeNotification// 界面旋轉(zhuǎn)通知姐浮,此時(shí)界面已經(jīng)完成旋轉(zhuǎn)UIApplicationDidChangeStatusBarOrientationNotification

判斷當(dāng)前界面方向

BOOL isPortrait=UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation]);BOOL isLandscape=UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation]);

單個(gè)頁面是否可以旋轉(zhuǎn)

@property(nonatomic,readonly)BOOL shouldAutorotateNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;@property(nonatomic,readonly)UIInterfaceOrientationMask supportedInterfaceOrientationsNS_AVAILABLE_IOS(6_0)__TVOS_PROHIBITED;

面向Orientation的信息:隨著屏幕的方向變化

[UISCreen bounds]

[UISCreen applicationFrame]

Status bar frame notifications

Keyboard frame notifications

屏幕旋轉(zhuǎn)時(shí)使得View有不同布局的實(shí)現(xiàn)方法

// way1-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{[superviewWillTransitionToSize:size withTransitionCoordinator:coordinator];[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}completion:^(id<UIViewControllerTransitionCoordinatorContext>_Nonnull context){}];}

// way2-(void)viewDidLayoutSubviews{[superviewDidLayoutSubviews];if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)){_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(50,120,150,50);}else{_testTextField.frame=CGRectMake(50,50,150,50);_testButton.frame=CGRectMake(220,50,150,50);}}

旋轉(zhuǎn)屏幕方向

NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];

需求:APP總體不跟隨設(shè)備的旋轉(zhuǎn)來改變界面的方向,個(gè)別頁面橫屏展示

實(shí)現(xiàn)思路:首先APP應(yīng)該支持橫屏和豎屏的展示垫言,也就是說在TARGETS中的Device Orientation選項(xiàng)勾選橫屏和豎屏選項(xiàng)。假設(shè)ViewController是根視圖瞻讽,那么當(dāng)ViewController的- (BOOL)shouldAutorotate方法返回為YES的時(shí)候鸳吸,界面的方向跟隨設(shè)備方向變化。在NV結(jié)構(gòu)(NavigationController上放置ViewController的結(jié)構(gòu))中需橫屏展示的ViewController可不是根視圖速勇,此時(shí)界面是否跟隨設(shè)備旋轉(zhuǎn)取決于根視圖NavigationController的- (BOOL)shouldAutorotate方法的返回值晌砾。在這種情況中,可重寫NavigationController的- (BOOL)shouldAutorotate方法返回Navigation棧中的頂部ViewController的shouldAutorotate烦磁,以此達(dá)到在單個(gè)頁面隨時(shí)控制其橫屏的效果养匈。橫屏通過Key-Value的方法修改設(shè)備方向來達(dá)到目的。

一都伪、勾選橫屏和豎屏選項(xiàng)

勾選橫屏和豎屏選項(xiàng)

二呕乎、重寫- (BOOL)shouldAutorotate返回棧頂控制器的shouldAutorotate

@interfaceMyNav:UINavigationController@end@implementationMyNav-(BOOL)shouldAutorotate{returnself.topViewController.shouldAutorotate;}@end

三、在將要橫屏顯示的ViewController中陨晶,重寫- (BOOL)shouldAutorotate猬仁,重點(diǎn)是用全局變量控制它的返回值

@interfaceLandscapeViewController(){BOOL _allowAutorotate;}-(BOOL)shouldAutorotate{[supershouldAutorotate];return_allowAutorotate;}

四、通過Key-Value改變設(shè)備方向

_allowAutorotate=YES;NSNumber*value0=[NSNumber numberWithInteger:UIDeviceOrientationUnknown];[[UIDevice currentDevice]setValue:value0 forKey:@"orientation"];NSNumber*value=[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft];[[UIDevice currentDevice]setValue:value forKey:@"orientation"];_allowAutorotate=NO;

轉(zhuǎn)載鏈接:http://www.reibang.com/p/3db323293428

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末先誉,一起剝皮案震驚了整個(gè)濱河市湿刽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌褐耳,老刑警劉巖诈闺,帶你破解...
    沈念sama閱讀 212,029評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異漱病,居然都是意外死亡买雾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評論 3 385
  • 文/潘曉璐 我一進(jìn)店門杨帽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漓穿,“玉大人,你說我怎么就攤上這事注盈』挝#” “怎么了?”我有些...
    開封第一講書人閱讀 157,570評論 0 348
  • 文/不壞的土叔 我叫張陵老客,是天一觀的道長僚饭。 經(jīng)常有香客問我,道長胧砰,這世上最難降的妖魔是什么鳍鸵? 我笑而不...
    開封第一講書人閱讀 56,535評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮尉间,結(jié)果婚禮上偿乖,老公的妹妹穿的比我還像新娘击罪。我一直安慰自己,他們只是感情好贪薪,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,650評論 6 386
  • 文/花漫 我一把揭開白布媳禁。 她就那樣靜靜地躺著,像睡著了一般画切。 火紅的嫁衣襯著肌膚如雪竣稽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,850評論 1 290
  • 那天霍弹,我揣著相機(jī)與錄音毫别,去河邊找鬼。 笑死典格,一個(gè)胖子當(dāng)著我的面吹牛拧烦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播钝计,決...
    沈念sama閱讀 39,006評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼恋博,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了私恬?” 一聲冷哼從身側(cè)響起债沮,我...
    開封第一講書人閱讀 37,747評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎本鸣,沒想到半個(gè)月后疫衩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,207評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡荣德,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,536評論 2 327
  • 正文 我和宋清朗相戀三年闷煤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片涮瞻。...
    茶點(diǎn)故事閱讀 38,683評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鲤拿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出署咽,到底是詐尸還是另有隱情近顷,我是刑警寧澤,帶...
    沈念sama閱讀 34,342評論 4 330
  • 正文 年R本政府宣布宁否,位于F島的核電站窒升,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏慕匠。R本人自食惡果不足惜饱须,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,964評論 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望台谊。 院中可真熱鬧蓉媳,春花似錦歹苦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狠角。三九已至号杠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間丰歌,已是汗流浹背姨蟋。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留立帖,地道東北人眼溶。 一個(gè)月前我還...
    沈念sama閱讀 46,401評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像晓勇,于是被迫代替她去往敵國和親堂飞。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,566評論 2 349

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