【问题标题】:Dismiss modal form sheet view on outside tap iOS 8关闭外部点击 iOS 8 上的模态表单视图
【发布时间】:2014-09-03 07:14:49
【问题描述】:

我一直试图在 iOS 8 上关闭模式表单视图,但没有成功, 我试过这段代码

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];
    }
 }
}

但它没有检测到外部视图点击,有什么建议吗?

【问题讨论】:

  • 我已经发布了我尝试过的代码的链接。
  • 贴出您的相关代码,而不是其他人的代码。此外,没有工作 不是有效的问题描述。
  • 更新问题,请看。
  • 我还没有找到任何解决方案,但我正在关注这个讨论:stackoverflow.com/questions/9102497/…

标签: ios objective-c ipad


【解决方案1】:

iOS 8 其实有两个问题。第一,手势识别没有开始。

我通过添加UIGestureRecognizerDelegate 协议并实现来解决了这个问题

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
    return YES;
}

另外,别忘了注册代理

recognizer.delegate = self;

现在手势识别器应该可以识别手势并且目标方法 (handleTapBehind:) 将被调用。

这是 iOS 8 中的第二个问题:如果 nil 作为视图传递,locationInView: 似乎没有考虑设备方向。相反,传递根视图是可行的。

这是我似乎适用于 iOS 7.1 和 8.0 的目标代码:

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];
        }];
    }
}

【讨论】:

  • 我按照您的步骤操作,它对我来说非常有效。但是,我不确定为什么我们首先需要添加“shouldRecognizeSimultaneouslyWithGestureRecognizer:”。如果我不返回“YES”,则无法识别模态视图的外侧。但是为什么和“RecognizeSimultaneouslyWithGestureRecognizer”有关系呢?
  • @DavidLiu 有另一个(系统)手势识别器处于活动状态,它优先。可能是一个错误。
  • 这是最好的解决方案。并解决了其他解决方案存在的旋转坐标问题。完美!
  • 我发现使用这个解决方案,点击导航栏也会丢弃它。
  • 您的解决方案与我的相同,但从 iOS8 开始,在表单表单全屏显示的小型 iPhone 上,横向将其全部丢弃。不知何故,在 iPhone 上,坐标的旋转根本不起作用。您可以点击全屏视图中的任意位置,它会消失。但仅限于景观。它仍然可以在 iPhone 6 Plus、iPad、iPhone 肖像等上完美运行。试图找到一种比 if (iphone) 或其他东西更好的检查方法。
【解决方案2】:

在 iOS 8 中,您可以查看使用新的 UIPresentationController 类。它使您可以更好地控制自定义视图控制器演示文稿周围的容器(允许您正确添加自己的手势识别器)。

这里还有一个非常简单的教程的链接:http://dativestudios.com/blog/2014/06/29/presentation-controllers/

然后添加调光视图点击关闭:

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.dimmingView addGestureRecognizer:singleFingerTap];


- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

【讨论】:

  • 我猜投反对票是因为本教程只讨论动画过渡,而不是关于 tab-outside 解雇功能。不清楚 UIPresentationController 类是否可以解决手头的问题(不阅读文档)。
  • 由于 UIPresentationController 是正确的方法,我更新了答案以包含所需的点击以关闭代码示例。 Apple 的示例代码非常好,而不是那篇博文:developer.apple.com/library/ios/featuredarticles/…
  • 我自己更喜欢这种方法,因为我曾经遇到过表单显示弹出框视图的实例,该视图的弹出框可能会超出所显示表单的边界 - 以及基于 view.window 的手势识别器如果您在表单边界之外的任何地方点击弹出框,将关闭视图。我宁愿调暗视图处理水龙头!
  • 在使用自定义 Modal Segue 和 UIPresentationController 时,在 iOS 12 中仍然是最好的方法。不要忘记添加self.dimmingView setUserInteractionEnabled:YES,否则gestureRecognizer 将无法捕获任何内容。
【解决方案3】:

适用于纵向和横向的 Swift 3.1 解决方案。

class TapBehindModalViewController: UIViewController, UIGestureRecognizerDelegate {
    private var tapOutsideRecognizer: UITapGestureRecognizer!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if(self.tapOutsideRecognizer == nil) {
            self.tapOutsideRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapBehind))
            self.tapOutsideRecognizer.numberOfTapsRequired = 1
            self.tapOutsideRecognizer.cancelsTouchesInView = false
            self.tapOutsideRecognizer.delegate = self
            self.view.window?.addGestureRecognizer(self.tapOutsideRecognizer)
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if(self.tapOutsideRecognizer != nil) {
            self.view.window?.removeGestureRecognizer(self.tapOutsideRecognizer)
            self.tapOutsideRecognizer = nil
        }
    }

    func close(sender: AnyObject) {
        self.dismiss(animated: true, completion: nil)
    }

    // MARK: - Gesture methods to dismiss this with tap outside
    func handleTapBehind(sender: UITapGestureRecognizer) {
        if (sender.state == UIGestureRecognizerState.ended) {
            let location: CGPoint = sender.location(in: self.view)

            if (!self.view.point(inside: location, with: nil)) {
                self.view.window?.removeGestureRecognizer(sender)
                self.close(sender: sender)
            }
        }
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2012-02-24
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多