touchesBegan功能永远不会被输入

时间:2015-12-30 10:46:04

标签: swift touchesbegan gmsmapview

我试图为我的应用弹出一个弹出窗口。到目前为止,弹出窗口弹出一个固定的坐标,我试图让它在用户点击的位置弹出。这就是我所拥有的:

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

}

我也看到了打印报表。不知道我在这里失踪了什么。

为superview和视图启用了用户交互:

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:4)

当您期望视图未接收到触摸时,有三个主要原因:

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

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

  • 感兴趣的观点不在其父母的范围内。

    命中测试以递归方式工作:窗口在每个子节点上调用hitTest(_:withEvent:)(按从上到下的顺序),直到一个返回非零值;如果hitTest(_:withEvent:)返回false,则pointInside(_:withEvent:)会立即返回nil;否则hitTest(_:withEvent:)会在子项上调用hitTest(_:withEvent:)(按从上到下的顺序),直到一个返回非零值,如果没有孩子报告命中,则返回self

    因此,如果一个孩子在其父母的界限之外,它可以是可见的(如果所有祖先都将clipsToBounds设置为假),但从未接受过触摸,因为其父亲的pointInside(_:withEvent:)将拒绝出现的触摸在孩子看来。

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

答案 1 :(得分:2)

结果显示Googlemaps&#39; GMSView在视图中使用其他手势,必须明确禁止它:

mapView.settings.consumesGesturesInView = false;

答案 2 :(得分:0)

如果userInteractionEnabled为真,则应调用touchesBegan,您的代码将正常工作,弹出窗口将显示在用户点击的位置。

如果您的视图是子视图,请确保在任何超级视图中userInteractionEnabled为true。有关touchesBegan未被调用的更多信息,请查看this answer