我试图使用类型为.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)
}
}
}
最佳答案
在要处理轻敲的userinteractionenable
上将true
设置为view
。还有第二件事,为什么要用viewDidAppear
做东西?您应该使用viewDidLoad
添加手势识别器。
更新:
self.view.window?.userInteractionEnabled = true
如果您正在使用导航控制器,则
self.navigationController?.view.window?.userInteractionEnabled = true
希望thiw会有所帮助:)
关于ios - 如何在 View 外部轻击时关闭iPad上的popover View Controller :IOS 9,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36887041/