【问题标题】:How to recognise a tap gesture outside of a modal dialog when dialog is open对话框打开时如何识别模态对话框外的点击手势
【发布时间】:2013-04-08 08:34:50
【问题描述】:

我只想知道当用户在模式对话框之外点击时如何识别。 我已经尝试过了,但是当用户在外面点击时它不会被调用。

这是我的 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];

        }

这不是打开handleTapBehind方法。

我已将模态视图控制器设为 UIGestureRecognition 的协议。

【问题讨论】:

    标签: ios objective-c ipad modal-dialog gesture-recognition


    【解决方案1】:

    这里有点晚了,但以防有人通过 Google 来到这里:

    设置手势识别器应该在视图出现之后进行。在 viewDidLoad 方法调用期间,视图的窗口为 nil,因此永远不会添加手势识别器。

    将方法调用放在 viewDidAppear 中,它应该可以按预期工作。

    【讨论】:

      【解决方案2】:

      您是否在 .h 文件中设置了 UIGestureRecognizerDelegate?

      您可以在Apple doc获取UIGestureRecognition行为的完整信息

      使用 UIGestureRecognizers 非常简单。您只需执行以下步骤: 创建手势识别器。创建手势识别器时,您需要指定一个回调方法,以便手势识别器可以在手势开始、更改或结束时向您发送更新。

      将手势识别器添加到视图中。每个手势识别器与一个(并且只有一个)视图相关联。当在该视图的边界内发生触摸时,手势识别器将查看它是否匹配它正在寻找的触摸类型,如果找到匹配,它将通知回调方法。

      您可以通过编程方式执行这两个步骤,但使用 Storyboard 编辑器直观地添加手势识别器更加容易。

      See Full tutorial and working here

      【讨论】:

      • 是的,我已经在 .h 文件中设置了 UIGestureRecognizerDelegate。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2011-06-18
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多