UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; [recognizer setNumberOfTapsRequired:1]; recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view [self.view.window addGestureRecognizer:recognizer]; - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window //Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view. if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) { // Remove the recognizer first so it's view.window is valid. [self.view.window removeGestureRecognizer:sender]; [self dismissModalViewControllerAnimated:YES]; } } }
iOS7.0 可行, iOS8.0 handleTap不走
加代理
UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer { return YES; }
recognizer.delegate = self;
if (sender.state == UIGestureRecognizerStateEnded) {
可运行UIView *rootView = self.view.window.rootViewController.view; CGPoint location = [sender locationInView:rootView]; if (![self.view pointInside:[self.view convertPoint:location fromView:rootView] withEvent:nil]) { [self dismissViewControllerAnimated:YES completion:^{ [self.view.window removeGestureRecognizer:sender]; }]; } }
原网页http://stackoverflow.com/questions/25638409/dismiss-modal-form-sheet-view-on-outside-tap-ios-8
iPad模态推出sheet,点击外部消失
最新推荐文章于 2024-07-18 19:36:20 发布