本次推出一个小功能: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];
}
}