IOS Present View Controller 詳解

第一:基礎的Present View Controller 解析

一、主要用途

彈出模態ViewController是iOS變成中很有用的一個技術,UIKit提供的一些專門用于模態顯示的ViewController,如UIImagePickerController等。彈出模態ViewController主要使用于一下這幾種情形:

1、收集用戶輸入信息

2、臨時呈現一些內容

3、臨時改變工作模式

4、相應設備方向變化(用于針對不同方向分別是想兩個ViewController的情況)

5、顯示一個新的view層級

這幾種情形都會暫時中斷程序正常的執行流程,主要作用是收集或者顯示一些信息。

二、幾個概念和常用設置

1、presenting view controller Vs presented view controller

當我們在view controller A中模態顯示view controller B的時候,A就充當presenting view controller(彈出VC),而B就是presented view controller(被彈出VC)。官方文檔建議這兩者之間通過delegate實現交互,如果使用過UIImagePickerController從系統相冊選取照片或者拍照,我們可以發現imagePickerController和彈出它的VC之間就是通過UIImagePickerControllerDelegate實現交互的。因此我們在實際應用用,最好也遵守這個原則,在被彈出的VC中定義delegate,然后在彈出VC中實現該代理,這樣就可以比較方便的實現兩者之間的交互。

2、Modal Presentation Styles(彈出風格)

通過設置presented VC的modalPresentationStyle屬性,我們可以設置彈出View Controller時的風格,有以下四種風格,其定義如下:

typedef enum {

UIModalPresentationFullScreen = 0,

UIModalPresentationPageSheet,

UIModalPresentationFormSheet,

UIModalPresentationCurrentContext,

} UIModalPresentationStyle;

UIModalPresentationFullScreen代表彈出VC時,presented VC充滿全屏,如果彈出VC的wantsFullScreenLayout設置為YES的,則會填充到狀態欄下邊,否則不會填充到狀態欄之下。

UIModalPresentationPageSheet代表彈出是彈出VC時,presented VC的高度和當前屏幕高度相同,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區域將會變暗并阻止用戶點擊,這種彈出模式下,豎屏時跟UIModalPresentationFullScreen的效果一樣,橫屏時候兩邊則會留下變暗的區域。

UIModalPresentationFormSheet這種模式下,presented VC的高度和寬度均會小于屏幕尺寸,presented VC居中顯示,四周留下變暗區域。

UIModalPresentationCurrentContext這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同。

這四種方式在iPad上面統統有效,但在iPhone和iPod touch上面系統始終已UIModalPresentationFullScreen模式顯示presented VC。

3、Modal Transition Style(彈出時的動畫風格)

通過設置設置presented VC的modalTransitionStyle屬性,我們可以設置彈出presented VC時場景切換動畫的風格,其定義如下:

typedef enum {

UIModalTransitionStyleCoverVertical = 0,

UIModalTransitionStyleFlipHorizontal,

UIModalTransitionStyleCrossDissolve,

UIModalTransitionStylePartialCurl,

} UIModalTransitionStyle;

我們可以看到有從底部滑入,水平翻轉進入,交叉溶解以及翻頁這四種風格可選。這四種風格在不受設備的限制,即不管是iPhone還是iPad都會根據我們指定的風格顯示轉場效果。

4、Dismiss Modal ViewController(消失彈出的VC)

消失presented VC,我們可以通過調用以下兩個函數中的任何一個來完成

dismissModalViewControllerAnimated:? ? ? ? ? ? ? ? // 將要廢棄,不贊成繼續使用

dismissViewControllerAnimated:completion:

誰來調用這消失presented VC的這個方法:正確的做法是“誰污染誰治理”,即presenting VC調用上面的方法來取消presented VC的顯示。這樣做有一個好處,如果一個VC真不用戶做的不同選擇可能彈出不同的view controller,當不再需要顯示被彈出的view controller的時候,直接調用[self dismissModalViewControllerAnimated]即可使之消失,而不用去關心其具體顯示的哪一類view controller。當然系統在這里做了優化,當我們在presented VC里面調用上面的方法的時候,系統會自動的將這個消息傳遞到相應的presenting VC中,這樣就可以實現不管誰彈出了自己,當不再需要的時候直接將自己消失掉的功能。在應用中具體要采用那種要看具體情況,如果presented VC需要和presenting VC有數據傳遞的話,建議在presenting VC實現的代理函數中dismiss彈出的view controller。

presentModalViewController支持5.0,而presentViewController支持5.0以下

你可以

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {

[self presentModalViewController:self.childVC animated:YES];

} else {

[self presentViewController:self.childVC animated:YES completion:nil];

}

或直接使用

[self presentViewController:self.childVC animated:YES completion:nil];

來解決兼容性問題

第二:高級自定義Present View Controller? 解析

我們在使用View跳轉的過程中,想使用各種各樣的特效,例如:翻頁、立方體等特效,我們就可以使用一下幾種方法,實現自己想要的動態的效果。

presentModalViewController:animated:completion:使用系統自帶四種動畫

簡單的實現方式:

[page2Controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentModalViewController:myNextViewController animated:YES? completion:nil];

系統支持的四種動畫:

typedef enum {

UIModalTransitionStyleCoverVertical=0, //默認方式,豎向上推

UIModalTransitionStyleFlipHorizontal, //水平反轉

UIModalTransitionStyleCrossDissolve,//隱出隱現

UIModalTransitionStylePartialCurl,//部分翻頁效果

} UIModalTransitionStyle;

presentModalViewController:animated:completion: 不用自帶的四種動畫效果

以下是我們高級自定義的動畫效果:

實現全翻頁效果:

CATransition *animation = [CATransition animation];

animation.duration = 1.0;

animation.timingFunction = UIViewAnimationCurveEaseInOut;

animation.type = @"pageCurl";

//animation.type = kCATransitionPush;

animation.subtype = kCATransitionFromLeft;

[self.view.window.layer addAnimation:animation forKey:nil];

[self presentModalViewController:myNextViewController animated:NO completion:nil];

常見的轉換類型(type):

kCATransitionFade? ? ? ? ? ? ? //淡出

kCATransitionMoveIn? ? ? ? ? //覆蓋原圖

kCATransitionPush? ? ? ? ? ? ? //推出

kCATransitionReveal? ? ? ? ? //底部顯出來

SubType:

kCATransitionFromRight

kCATransitionFromLeft? ? // 默認值

kCATransitionFromTop

kCATransitionFromBottom

設置其他動畫類型的方法(type):

pageCurl? 向上翻一頁

pageUnCurl 向下翻一頁

rippleEffect 滴水效果

suckEffect 收縮效果,如一塊布被抽走

cube 立方體效果

oglFlip 上下翻轉效果

總之,我們使用這些靜態的ViewController過程中,實現更多的動畫效果,就需要深入了解它的底層,了解更深入的IOS底層內容。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容