model 出来一半 viewController(控制器) 的实现方法

本次推出一个小功能:Model出来一半 view,点击一半 view 的以外区域,dismiss 掉这个 model 出来的 view

这里有一个小问题,如果只 model 一半屏幕的 view,model 后屏幕会变黑,这里需要在 model 出来的 view中,这是他的的属性中

modelVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

废话少说上代码,首先是按钮的点击事件-->model 出控制器的按钮

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

加上这一句就不会变黑了

设置model 出来的 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];

    
}

设置手指范围在透明view 区域,才 dissmiss 调 model 出来的 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

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,601评论 25赞 708
  • 记得小时候有篇课文《小猫钓鱼》,故事讲的是小猫咪在钓鱼过程中,不是专心钓鱼,而是一会儿捉蜻蜓,一会儿追蝴蝶,三心二...
    村长下乡阅读 360评论 0赞 0
  • https://github.com/corpsepiges/leetcode
    keaidelele阅读 481评论 0赞 50
  • 成长训练营的第二天,听到Grace关于高效阅读的分享一下子让我明白了阅读的内容原来不仅仅是信件,文章,邮件等实...
    W王航阅读 331评论 1赞 2
  • 我和你认识8年,成为最好的闺蜜用了5年,彼此冷落着对方已经三年或许可能会继续。 我们相识在2009年,你给我的第一...
    我在七月里阅读 531评论 0赞 0

友情链接更多精彩内容