所以我通常按照这种方式在其视图外点击时忽略ModalViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
{
if(![self.view.window.gestureRecognizers containsObject:recognizer])
{
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];
[recognizer release];
}
}
}
- (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])
{
[self dismissModalViewControllerAnimated:YES];
[self.view.window removeGestureRecognizer:recognizer];
}
}
}
它与常规 ModalViewController工作良好..但现在我有一个modelViewController,它是一个NavigationViewController,以“x”viewController作为根目录..当我使用这种方式并点击navigationBar时控制器被解雇,或者当我从x控制器导航到“y”控制器并想要回来时,当我单击后退按钮时,ModalView被解除!这对我来说是错误的行为..我只是希望控制器被解雇,以防水龙头完全在控制器外(查看区域+导航栏区域)..任何人都可以提供帮助吗?
答案 0 :(得分:4)
更新以下代码:
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint flocation = [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.
CGPoint tap = [self.view convertPoint:flocation fromView:self.view.window];
CGPoint tapBar = [self.navigationController.navigationBar convertPoint:flocation fromView:self.view.window];
if (![self.view pointInside:tap withEvent:nil] && ![self.navigationController.navigationBar pointInside:tapBar withEvent:nil])
{
// Remove the recognizer first so it's view.window is valid.
[self.view.window removeGestureRecognizer:sender];
[self dismissModalViewControllerAnimated:YES];
}
}
}
点击UIBarButtonItem时从窗口中删除UITapGestureRecognizer
-(void)viewWillDisappear:(BOOL)animated {
[self.view.window removeGestureRecognizer:self.tapOutsideGestureRecognizer];
}