一种吸、主要用途
彈出模態(tài)ViewController是iOS變成中很有用的一個技術(shù)嫉戚,UIKit提供的一些專門用于模態(tài)顯示的ViewController,如UIImagePickerController等瓣俯。彈出模態(tài)ViewController主要使用于一下這幾種情形:
1俊马、收集用戶輸入信息
2、臨時呈現(xiàn)一些內(nèi)容
3敷存、臨時改變工作模式
4墓造、相應(yīng)設(shè)備方向變化(用于針對不同方向分別是想兩個ViewController的情況)
5、顯示一個新的view層級
這幾種情形都會暫時中斷程序正常的執(zhí)行流程锚烦,主要作用是收集或者顯示一些信息觅闽。
二、幾個概念和常用設(shè)置
1涮俄、presenting view controller Vs presented view controller
當(dāng)我們在view controller A中模態(tài)顯示view controller B的時候蛉拙,A就充當(dāng)presenting view controller(彈出VC),而B就是presented view controller(被彈出VC)彻亲。官方文檔建議這兩者之間通過delegate實(shí)現(xiàn)交互孕锄,如果使用過UIImagePickerController 從系統(tǒng)相冊選取照片或者拍照,我們可以發(fā)現(xiàn)imagePickerController和彈出它的VC之間就是通過 UIImagePickerControllerDelegate實(shí)現(xiàn)交互的苞尝。因此我們在實(shí)際應(yīng)用用硫惕,最好也遵守這個原則,在被彈出的VC中定義 delegate野来,然后在彈出VC中實(shí)現(xiàn)該代理,這樣就可以比較方便的實(shí)現(xiàn)兩者之間的交互踪旷。
2曼氛、Modal Presentation Styles(彈出風(fēng)格)
通過設(shè)置presenting VC的modalPresentationStyle屬性豁辉,我們可以設(shè)置彈出View Controller時的風(fēng)格,有以下四種風(fēng)格舀患,其定義如下:
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
UIModalPresentationFullScreen代表彈出VC時徽级,presented VC充滿全屏,如果彈出VC的wantsFullScreenLayout設(shè)置為YES的聊浅,則會填充到狀態(tài)欄下邊餐抢,否則不會填充到狀態(tài)欄之下。
UIModalPresentationPageSheet代表彈出是彈出VC時低匙,presented VC的高度和當(dāng)前屏幕高度相同旷痕,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區(qū)域?qū)儼挡⒆柚褂脩酎c(diǎn)擊顽冶,這種彈出模式下欺抗,豎屏?xí)r跟 UIModalPresentationFullScreen的效果一樣,橫屏?xí)r候兩邊則會留下變暗的區(qū)域强重。
UIModalPresentationFormSheet這種模式下绞呈,presented VC的高度和寬度均會小于屏幕尺寸,presented VC居中顯示间景,四周留下變暗區(qū)域佃声。
UIModalPresentationCurrentContext這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同倘要。
這四種方式在iPad上面統(tǒng)統(tǒng)有效圾亏,但在iPhone和iPod touch上面系統(tǒng)始終已UIModalPresentationFullScreen模式顯示presented VC。
3碗誉、Modal Transition Style(彈出時的動畫風(fēng)格)
通過設(shè)置設(shè)置presenting VC的modalTransitionStyle屬性召嘶,我們可以設(shè)置彈出presented VC時場景切換動畫的風(fēng)格,其定義如下:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
我們可以看到有從底部滑入哮缺,水平翻轉(zhuǎn)進(jìn)入弄跌,交叉溶解以及翻頁這四種風(fēng)格可選。這四種風(fēng)格在不受設(shè)備的限制尝苇,即不管是iPhone還是iPad都會根據(jù)我們指定的風(fēng)格顯示轉(zhuǎn)場效果铛只。
4、Dismiss Modal ViewController(消失彈出的VC)
消失presented VC糠溜,我們可以通過調(diào)用以下兩個函數(shù)中的任何一個來完成
dismissModalViewControllerAnimated: // 將要廢棄淳玩,不贊成繼續(xù)使用
dismissViewControllerAnimated:completion:
誰來調(diào)用這消失presented VC的這個方法:正 確的做法是“誰污染誰治理”,即presenting VC調(diào)用上面的方法來取消presented VC的顯示非竿。這樣做有一個好處蜕着,如果一個VC真不用戶做的不同選擇可能彈出不同的view controller,當(dāng)不再需要顯示被彈出的view controller的時候,直接調(diào)用[self dismissModalViewControllerAnimated]即可使之消失承匣,而不用去關(guān)心 其具體顯示的哪一類view controller蓖乘。當(dāng)然系統(tǒng)在這里做了優(yōu)化,當(dāng)我們在presented VC里面調(diào)用上面的方法的時候韧骗,系統(tǒng)會自動的將這個消息傳遞到相應(yīng)的presenting VC中嘉抒,這樣就可以實(shí)現(xiàn)不管誰彈出了自己,當(dāng)不再需要的時候直接將自己消失掉的功能袍暴。在應(yīng)用中具體要采用那種要看具體情況些侍,如果presented VC需要和presenting VC有數(shù)據(jù)傳遞的話,建議在presenting VC實(shí)現(xiàn)的代理函數(shù)中dismiss彈出的view controller政模。
firstViewController *second = [[firstViewControlleralloc] init];
second.modalPresentationStyle=UIModalPresentationOverFullScreen;
second.modalTransitionStyle =UIModalTransitionStylePartialCurl;//彈出時的動畫風(fēng)格對彈出目標(biāo)頁面設(shè)置
/*
UIModalTransitionStyleCoverVertical,從下到上
UIModalTransitionStyleFlipHorizontal ,漸變
UIModalTransitionStyleCrossDissolve,旋轉(zhuǎn)
UIModalTransitionStylePartialCurl 翻頁效果
*/
[selfpresentViewController:second animated:YEScompletion:^{
NSLog(@"");
}];