iOS 模態(tài)彈出半透明界面

記錄工程中遇到的變態(tài)產(chǎn)品的變態(tài)問題,吧啦吧啦……
搜索的資料,以及實驗,貌似只能iOS 8+適用,不過iOS 7已經(jīng)趨近淘汰,先不考慮了咯……

一 彈出半透明界面

先上效果圖:



彈出ViewController

彈出NavigationController

其實真的很簡單,直接上代碼吧

//彈出ViewController
XXxViewController *xVC = [XXxViewController new];
//設(shè)置ViewController的背景顏色及透明度
xVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
//設(shè)置ViewController的模態(tài)模式,即ViewController的顯示方式
xVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
//加載模態(tài)視圖
[self presentViewController:xVC animated:YES completion:^{
    }];

彈出NavigationController這個不用多解釋吧,當(dāng)你看到這個帖子的需求的時候,我相信你已經(jīng)會把ViewController包一層NavigationController模態(tài)彈出了。

//彈出NavigationController
XXxViewController *xVC = [XXxViewController new];
//設(shè)置ViewController的背景顏色及透明度
xVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
//設(shè)置NavigationController根視圖
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:xVC];
//設(shè)置NavigationController的模態(tài)模式,即NavigationController的顯示方式
navigation.modalPresentationStyle = UIModalPresentationOverCurrentContext;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
//加載模態(tài)視圖
[self presentViewController:navigation animated:YES completion:^{
    }];

Over完成,試試吧,其實真的很簡單!

總結(jié)
關(guān)于模態(tài)的modalPresentationStyle解釋。(嗯,我也是粘別人的~)
UIModalPresentationStyle即viewcontroller的顯示方式

typedefNS_ENUM(NSInteger, UIModalPresentationStyle) {
        UIModalPresentationFullScreen =0,//由下到上,全屏覆蓋
        UIModalPresentationPageSheet,//在portrait時是FullScreen,在landscape時和FormSheet模式一樣。
        UIModalPresentationFormSheet,// 會將窗口縮小,使之居于屏幕中間。在portrait和landscape下都一樣,但要注意landscape下如果軟鍵盤出現(xiàn),窗口位置會調(diào)整。
        UIModalPresentationCurrentContext,//這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同。
        UIModalPresentationCustom,//自定義視圖展示風(fēng)格,由一個自定義演示控制器和一個或多個自定義動畫對象組成。符合UIViewControllerTransitioningDelegate協(xié)議。使用視圖控制器的transitioningDelegate設(shè)定您的自定義轉(zhuǎn)換。
        UIModalPresentationOverFullScreen,//如果視圖沒有被填滿,底層視圖可以透過
        UIModalPresentationOverCurrentContext,//視圖全部被透過
        UIModalPresentationPopover,
        UIModalPresentationNone ,
    };

二 自定義彈出動畫

記錄工程中遇到的變態(tài)產(chǎn)品的變態(tài)問題 too,吧啦吧啦……
對于系統(tǒng)的模態(tài)彈出的方式只有四種:

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
UIModalTransitionStyleCoverVertical = 0, // 底部滑入。
UIModalTransitionStyleFlipHorizontal, // 水平翻轉(zhuǎn)。
UIModalTransitionStyleCrossDissolve, // 交叉溶解。
UIModalTransitionStylePartialCurl, // 翻頁。
};

氮素?fù)醪蛔‘a(chǎn)品(gou)想從上往下彈出,來吧,我們直接寫動畫吧~~
彈出

//自定義動畫
CATransition *animation = [CATransition animation];
animation.duration = 0.3;
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromBottom;
[self.view.window.layer addAnimation:animation forKey:nil];
//彈出ViewController
XXxViewController *xVC = [XXxViewController new];
[self presentViewController:xVC animated:YES completion:^{
    }];

消失(原路返回消失)

CATransition *animation = [CATransition animation];
animation.duration = 0.3;
animation.type = kCATransitionReveal;
animation.subtype = kCATransitionFromTop;
[self.view.window.layer addAnimation:animation forKey:nil];
[self dismissViewControllerAnimated:NO completion:0];

總結(jié)
關(guān)于type和subtype:
setType:有四種類型:
kCATransitionFade //交叉淡化過渡
kCATransitionMoveIn //移動覆蓋原圖
kCATransitionPush //新視圖將舊視圖推出去
kCATransitionReveal //底部顯出來

Subtype:有四種類型:
kCATransitionFromRight
kCATransitionFromLeft (默認(rèn)值)
kCATransitionFromTop
kCATransitionFromBottom

目前內(nèi)容已經(jīng)完成,有什么好的建議歡迎討論O(∩_∩)O哈哈哈~

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

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

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