如何在iPad上点击视图外部时关闭弹出视图控制器:IOS 9

3

我正在尝试使用类型为.pagesheet的模态样式将视图控制器呈现为popoverviewcontroller。在这里,我正在尝试添加一个轻拍手势识别器,以便在单击其视图外部时关闭此popoverviewcontroller。但是在iOS 9中无法检测到轻拍手势。下面是手势识别器的代码:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        let recog : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action:#selector(HandleTap))
        recog.numberOfTapsRequired = 1
        recog.numberOfTouchesRequired = 1
        recog.cancelsTouchesInView = false
        recog.delegate = self
        self.view.window?.addGestureRecognizer(recog)

    }


func HandleTap(sender:UITapGestureRecognizer) -> Void
{
    if(sender.state == UIGestureRecognizerState.Ended)
    {
        var location : CGPoint = sender.locationInView(self.presentingViewController?.view)
        //var location : CGPoint = sender.locationInView(self.view?.window)
        if(!(self.view.pointInside(self.view.convertPoint(location, toView: self.view?.window), withEvent: nil)))
        {
          self.view.window?.removeGestureRecognizer(sender)
          self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
}
1个回答

1
userinteractionenable设置为true,在您处理点击的view上。另外,为什么要在viewDidAppear中执行操作?您应该使用viewDidLoad添加手势识别器。
self.view.window?.userInteractionEnabled = true

如果您正在使用导航控制器,则:
self.navigationController?.view.window?.userInteractionEnabled = true

希望这会有所帮助 :)

用户交互已经通过.xib文件在视图上启用。 - Shanbhag Vinit
如果您要将手势识别器添加到窗口,则应启用窗口的用户交互。从.xib文件中,您只能为视图启用用户交互。 - Ketan Parmar
handleTap 中加入打印语句,以便您知道该方法是否被调用。 - Ketan Parmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接