简体   繁体   English

呈现模态视图控制器时呈现视图控制器响应触摸事件

[英]Presenting view controller response touch events when modal view controller is presented

Here is the code: 这是代码:

UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
UIViewController *presentedViewController = [myStoryboard instantiateViewControllerWithIdentifier:@"IdentifierName"];
presentedViewController.modalPresentationStyle = UIModalPresentationFormSheet;

presentingViewController presentViewController:presentedViewController animated:NO completion:nil];

the view of presentingViewController would not response any touch event, but I want it to take touch events,what should I do? presentingViewController的视图不会响应任何触摸事件,但是我希望它接受触摸事件,我该怎么办? Appreciate for any suggestion! 感谢任何建议!

This is the expected behavior and should be what you see in most apps. 这是预期的行为,应该是您在大多数应用中看到的。 Apple's App Store app does allow you to tap outside the modal form sheet to dismiss it, however, so there's some precedent for it being acceptable, in a limited way. Apple的App Store应用程序确实允许您在模式表单之外点击以将其关闭,因此,在有限的范围内,有一些先例可以接受。 If your design absolutely requires it, consider setting up and tearing down a window-based gesture recognizer in the presented view controller. 如果您的设计绝对需要,请考虑在提供的视图控制器中设置和拆除基于窗口的手势识别器。

For example, to implement the tap-outside-to-dismiss behavior, configure a tap gesture recognizer in viewDidAppear: . 例如,要实现轻拍从外部到关闭的行为,请在viewDidAppear:配置轻viewDidAppear:手势识别器。 You can remove it on viewWillDisappear: if it still exists at the time that is called. 您可以在viewWillDisappear:上将其删除viewWillDisappear:如果在调用时它仍然存在。

In the tap handler, you'd want to get the location of the tap in window coordinates, ie: 在轻敲处理程序中,您想获取轻敲在窗口坐标中的位置,即:

CGPoint tapPoint = [recognizer locationInView:nil];

You could then do some work with geometry to check and see if the tap occurred on the modally-presented view or outside (over the dimmed region that corresponds to your presenting view controller). 然后,您可以对几何图形进行一些检查,以查看是否在模态表示的视图上或外部(在与您的呈现视图控制器相对应的变暗区域上)发生了敲击。 UIView has a pointInside:withEvent: method that might help with that. UIView有一个pointInside:withEvent:方法可能对此有所帮助。

Here is the simple way to present viewcontroller... 这是呈现ViewController的简单方法...

  ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
  [self presentViewController:view animated:YES completion:nil];

Enjoy code 享受代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么模式视图(由详细视图控制器呈现)呈现视图控制器是拆分视图控制器? - Why modal view's (presented by detail view controller) presenting view controller is split view controller? 出现模态视图 controller 时暂停 animation - Pause animation when modal view controller is presented 呈现模式视图控制器时的异常:NSInternalInconsistencyException - Exception when presenting modal view controller: NSInternalInconsistencyException 何时在模态视图控制器上设置父/呈现视图控制器? - When is the parent/presenting view controller set on a modal view controller? 调用viewWillAppear时显示视图控制器的问题被解除 - Issue with calling viewWillAppear of presenting view controller when presented one is dismissed 消除演示的VC时,Presenting View Controller失去子视图 - Presenting View Controller loses subviews when dismissing presented VC 呈现视图控制器之上的模态视图控制器 - Modal view controller over Presenting view controller QLPreviewController在其上显示模态视图控制器后失去联系 - QLPreviewController loses touch after presenting a modal view controller over it 使呈现的视图控制器忽略并传递触摸事件 - Make presented view controller to ignore and pass through touch events 延迟呈现模式视图控制器 - Delay in presenting a modal view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM