本次推出一個(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];
}
}