我正在尝试弹出我的应用程序的弹出窗口。到目前为止,弹出窗口是在一个固定的坐标上弹出的,我正在尝试让它在用户点击的地方弹出。这就是我所拥有的:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesbegan")
for touch in touches{
//Handle touch
let location = touch.locationInView(self.view)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ColonyPopoverController") as! ColonyPopoverController
vc.modalPresentationStyle = .Popover
vc.preferredContentSize = CGSizeMake(200, 150)
if let popoverController = vc.popoverPresentationController {
popoverController.delegate = self
popoverController.sourceRect = CGRectMake(location.x, location.y, 20, 10)
popoverController.sourceView = self.view
self.presentViewController(vc, animated: true, completion: nil)
}
}
}我注意到当我点击模拟器时,print语句永远不会打印出来。
我在视图中启用了interaction和multi-touch。我知道这很好用,因为我还将它与Google Maps集成在一起,这样当我点击时,google图钉就会出现:
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView}
我还看到print语句也在打印。不知道我错过了什么。
superview和视图都启用了用户交互:


发布于 2015-12-31 12:10:34
事实证明,Googlemaps的GMSView消耗了视图中的其他手势,这必须明确禁止:
mapView.settings.consumesGesturesInView = false;发布于 2015-12-31 01:42:09
有三个主要原因导致视图在您期望的时候不能被访问:
userInteractionEnabled设置为false。显然你已经证实了事实并非如此。userInteractionEnabled设置为false。命中测试以递归方式工作:窗口对每个子级调用hitTest(_:withEvent:) (从上到下),直到返回非零;如果pointInside(_:withEvent:)返回false,则hitTest(_:withEvent:)立即返回nil;否则,hitTest(_:withEvent:)对子级调用hitTest(_:withEvent:) (从上到下),直到返回非零,如果没有子级报告命中,则返回self。
因此,如果子对象在其父对象的边界之外,它可以是可见的(如果所有祖先都将clipsToBounds设置为false),但永远不会接收到接触,因为其父对象的pointInside(_:withEvent:)将拒绝出现在子视图上的接触。
您可以通过使用Xcode's “Debug View Hierarchy” feature检查视图层次结构来诊断最后两种情况。
发布于 2015-12-30 20:10:53
如果userInteractionEnabled为true,则应调用touchesBegan,您的代码将正常工作,并在用户点击的位置显示弹出窗口。
如果你的视图是一个子视图,那么确保userInteractionEnabled在所有的超视图上都是真的。有关touchesBegan未被调用的更多信息,请查看this answer 。
https://stackoverflow.com/questions/34527946
复制相似问题