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

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

一 彈出半透明界面

先上效果圖:



彈出ViewController

彈出NavigationController

其實(shí)真的很簡單,直接上代碼吧

//彈出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這個(gè)不用多解釋吧,當(dāng)你看到這個(gè)帖子的需求的時(shí)候,我相信你已經(jīng)會(huì)把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完成,試試吧,其實(shí)真的很簡單!

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

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

二 自定義彈出動(dòng)畫

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

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

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

//自定義動(dòng)畫
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 //移動(dòng)覆蓋原圖
kCATransitionPush //新視圖將舊視圖推出去
kCATransitionReveal //底部顯出來

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

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

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

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

  • 翻譯自“View Controller Programming Guide for iOS”。 1 彈出視圖控制器...
    lakerszhy閱讀 3,776評(píng)論 2 20
  • 前言的前言 唐巧前輩在微信公眾號(hào)「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各項(xiàng)指標(biāo)...
    VincentHK閱讀 5,566評(píng)論 3 44
  • 2017.08.11 周五 晴 今天下午我和哥哥、同學(xué)許誠昊一起去游泳。來到男更衣室,我們脫掉衣服和褲子,換上泳褲...
    超速戰(zhàn)狼_小燦閱讀 463評(píng)論 2 6
  • 在生活中你經(jīng)常抱怨嗎? 這個(gè)問題讓我很為難,因?yàn)槲覍儆谔^樂觀的人,沒心沒肺,上梁不正下梁歪啊,父母都這樣特別是老...
    aa5db2ee5000閱讀 640評(píng)論 3 2

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