简体   繁体   English

打开对话框时如何识别模态对话框外的轻击手势

[英]How to recognise a tap gesture outside of a modal dialog when dialog is open

All I want to know is how to I recognise when a user taps outside of the modal dialog. 我只想知道当用户在模式对话框之外点击时如何识别。 I have tried this but it is not being called when the user taps outside. 我已经尝试过了,但是当用户在外面轻按时不会被调用。

Here is my viewDidLoad method which resides in the ModalDialogViewController.m file UITapGestureRecognizer *recognizer; 这是我的viewDidLoad方法,它位于ModalDialogViewController.m文件UITapGestureRecognizer * recognizer中;

        if(![self.view.window.gestureRecognizers containsObject:recognizer])
        {
            recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
            //[recognizer setDelegate:self];
            [recognizer setNumberOfTapsRequired:1];
            recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
            [self.view.window addGestureRecognizer:recognizer];

        }

This is not opening the handleTapBehind method. 这不会打开handleTapBehind方法。

I have made the modal view controller a protocol of UIGestureRecognition. 我已经将模态视图控制器作为UIGestureRecognition的协议。

A bit late here, but just incase someone comes here by way of Google: 这里有点晚,但以防万一有人通过Google来到这里:

Setting up the gesture recognizer should happen after the view appears. 视图显示后应进行手势识别器的设置。 During the viewDidLoad method invocation, the view's window is nil, so the gesture recognizer never gets added. 在viewDidLoad方法调用期间,视图的窗口为nil,因此永远不会添加手势识别器。

Put the method call in viewDidAppear and it should work as expected. 将方法调用放入viewDidAppear中,它应该可以正常工作。

Have you set UIGestureRecognizerDelegate in you .h file? 您是否已在.h文件中设置UIGestureRecognizerDelegate?

You can get full information of behavior of UIGestureRecognition in Apple doc 您可以在Apple文档中获取有关UIGestureRecognition行为的完整信息。

Using UIGestureRecognizers is extremely simple. 使用UIGestureRecognizers非常简单。 You just perform the following steps: Create a gesture recognizer. 您只需执行以下步骤:创建一个手势识别器。 When you create a gesture recognizer, you specify a callback method so the gesture recognizer can send you updates when the gesture starts, changes, or ends. 创建手势识别器时,可以指定一种回调方法,以便手势识别器可以在手势开始,更改或结束时向您发送更新。

Add the gesture recognizer to a view. 将手势识别器添加到视图。 Each gesture recognizer is associated with one (and only one) view. 每个手势识别器都与一个(只有一个)视图关联。 When a touch occurs within the bounds of that view, the gesture recognizer will look to see if it matches the type of touch it's looking for, and if a match is found it will notify the callback method. 当在该视图的边界内发生触摸时,手势识别器将查看其是否与要查找的触摸类型相匹配,如果找到匹配项,它将通知回调方法。

You can perform these two steps programatically , but it's even easier adding a gesture recognizer visually with the Storyboard editor. 您可以通过编程方式执行这两个步骤,但是使用Storyboard编辑器在视觉上添加手势识别器甚至更加容易。

See Full tutorial and working here : 请参阅完整教程并在这里工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM