2015-12-30 74 views
0

我试图弹出一个popover为我的应用程序。到目前为止,弹出窗口弹出一个固定的坐标,我试图让它弹出到用户点击的位置。这是我的:touchesBegan函数永远不会被输入

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) 
     } 
    } 
} 

我注意到,打印语句从不打印,当我点击模拟器。

我在我看来有interactionmulti-touch启用。我知道这工作得很好,因为我也有它与谷歌地图整合,所以,当我点击,会出现一个谷歌的针:

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 

}

,我看到了打印报表打印为好。不知道我在这里错过了什么。

用户交互是为上海华和视图均启用

enter image description here

enter image description here

回答

2

原来的GoogleMaps' GMSView消耗等手势,在视图,它必须被明令禁止:

mapView.settings.consumesGesturesInView = false; 
0

如果userInteractionEnabled为真,那么的touchesBegan应该叫你的代码将工作和弹出窗口将出现在用户点击。

如果您的视图是子视图,请确保userInteractionEnabled在任何超级视图上均为true。看看this answer更多有关touches的信息不会被调用。

+0

谢谢您的回答。超级视图和视图都启用了用户交互,附加了屏幕截图和更新后的问题。 – Siddhartha

+1

我找到了原因。 Googlemaps的GMSView在视图中使用其他手势,并且必须禁止:'''mapView.settings.consumesGesturesInView = false;'''。 – Siddhartha

3

还有为什么当你期望一个视图不接受触摸主要原因有三:

  • userInteractionEnabled设置为false。显然你已经证实情况并非如此。

  • 其他一些观点是“感兴趣的观点之上”,并且正在接受关注。请注意,子视图位于其父级(位于子级覆盖的区域)之上,故事板文档大纲中的视图位于大纲中较高级别的所有兄弟之上。 (在您的屏幕截图中,KittyMapper®位于地图视图顶部,它们重叠的任何区域。)如果您不希望顶部视图接收到触摸,则需要将userInteractionEnabled设置为false。

  • 感兴趣的视图不在其父项的范围之内。

    命中测试递归地工作:窗口调用hitTest(_:withEvent:)对其每个孩子(按照从上到下的顺序),直到返回非零;如果pointInside(_:withEvent:)返回false,则hitTest(_:withEvent:)立即返回零;否则hitTest(_:withEvent:)在孩子(从上至下的顺序)上调用hitTest(_:withEvent:),直到返回非零,并且如果没有儿童报告命中,则返回self。因此,如果一个孩子在父母的界限之外,它可以是可见的(如果所有祖先的clipsToBounds设置为false),但从不接触触摸,因为其父母的pointInside(_:withEvent:)将拒绝看起来在子视图上的触摸。

您可以使用Xcode's “Debug View Hierarchy” feature检查您的视图层次结构来诊断最后两种情况。

+0

感谢您的回答。我使用了gestureRecognizer,并注意到了相同的行为。我相信我的地图是用''''didTapAtCoordinate'''方法重写触摸识别器的。我不知道这两个是否可以结合使用。要试用'''Debug View Hierarchy''''功能,谢谢。 – Siddhartha

+0

我找到了原因。 Googlemaps的GMSView在视图中使用其他手势,并且必须禁止:'''mapView.settings.consumesGesturesInView = false;'''。 – Siddhartha

相关问题