model 出來(lái)一半 viewController(控制器) 的實(shí)現(xiàn)方法

本次推出一個(gè)小功能:Model出來(lái)一半 view,點(diǎn)擊一半 view 的以外區(qū)域,dismiss 掉這個(gè) model 出來(lái)的 view

這里有一個(gè)小問(wèn)題,如果只 model 一半屏幕的 view,model 后屏幕會(huì)變黑,這里需要在 model 出來(lái)的 view中,這是他的的屬性中

modelVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

廢話(huà)少說(shuō)上代碼,首先是按鈕的點(diǎn)擊事件-->model 出控制器的按鈕

- (IBAction)ModelDidClick:(id)sender {
    
    DSWModelViewController *modelVC = [[DSWModelViewController alloc]init];
    
    modelVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
    [self presentViewController:modelVC animated:YES completion:nil];
}

加上這一句就不會(huì)變黑了

設(shè)置model 出來(lái)的 VC 的 UI

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.alpha = 0.3;
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2)];
//    redView.userInteractionEnabled = NO;
    self.fenView=redView;
    [self.view addSubview:redView];
    
    redView.backgroundColor = [UIColor redColor];

    
}

設(shè)置手指范圍在透明view 區(qū)域,才 dissmiss 調(diào) model 出來(lái)的 view

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
      UITouch *touch=touches.anyObject;
      CGPoint currentPoint=[touch locationInView:self.view];

    if (!CGRectContainsPoint(self.fenView.frame, currentPoint)) {
        [self dismissViewControllerAnimated:YES completion:nil];

    }
    
}

下面是 github 的地址:https://github.com/D-james/ModelHalfViewController

最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,234評(píng)論 25 708
  • 記得小時(shí)候有篇課文《小貓釣魚(yú)》,故事講的是小貓咪在釣魚(yú)過(guò)程中,不是專(zhuān)心釣魚(yú),而是一會(huì)兒捉蜻蜓,一會(huì)兒追蝴蝶,三心二...
    村長(zhǎng)下鄉(xiāng)閱讀 325評(píng)論 0 0
  • https://github.com/corpsepiges/leetcode
    keaidelele閱讀 472評(píng)論 0 50
  • 成長(zhǎng)訓(xùn)練營(yíng)的第二天,聽(tīng)到Grace關(guān)于高效閱讀的分享一下子讓我明白了閱讀的內(nèi)容原來(lái)不僅僅是信件,文章,郵件等實(shí)...
    W王航閱讀 321評(píng)論 1 2
  • 我和你認(rèn)識(shí)8年,成為最好的閨蜜用了5年,彼此冷落著對(duì)方已經(jīng)三年或許可能會(huì)繼續(xù)。 我們相識(shí)在2009年,你給我的第一...
    我在七月里閱讀 514評(píng)論 0 0

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