touchesBegan 函数从未被输入
touchesBegan function never being entered
我正在尝试为我的应用程序弹出一个弹出窗口。到目前为止,弹出窗口在固定坐标处弹出,我试图让它在用户点击的位置弹出。这是我的:
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)
}
}
}
我注意到当我点击模拟器时打印语句从不打印。
我在视图中启用了 interaction
和 multi-touch
。我知道这很好用,因为我还将它与 Google 地图集成在一起,因此当我点击时,会出现一个 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
}
而且我也看到了打印语句。不确定我在这里遗漏了什么。
为超级视图和视图启用了用户交互:
如果 userInteractionEnabled
为真,则应调用 touchesBegan,您的代码将起作用,弹出窗口将出现在用户点击的位置。
如果您的视图是子视图,请确保 userInteractionEnabled 在任何父视图上都为真。查看 了解有关未调用 touchesBegan 的更多信息。
视图没有在您期望的时候接收到触摸的主要原因有以下三个:
已将 userInteractionEnabled
设置为 false。 Apparent只是您已经证实情况并非如此。
其他一些视图在感兴趣的视图“之上”,并且正在接收触摸。请注意,child 视图位于其 parent 之上(在 child 覆盖的区域中),故事板文档大纲中较低的视图位于任何兄弟姐妹之上在大纲中更高。 (在您的屏幕截图中,KittyMapper® 在地图视图的顶部,在它们重叠的任何区域。)如果您不希望顶部视图接收触摸,则需要将 userInteractionEnabled
设置为 false。
感兴趣的视图超出其 parent 的范围。
命中测试以递归方式工作:window 对其每个 children(按 top-to-bottom 顺序)调用 hitTest(_:withEvent:)
,直到一个 returns non-nil; hitTest(_:withEvent:)
returns 如果 pointInside(_:withEvent:)
returns false 立即为零;否则 hitTest(_:withEvent:)
在 child 上调用 hitTest(_:withEvent:)
(按 top-to-bottom 顺序)直到一个 returns non-nil 和 returns self
如果 children 的 none 报告命中。
因此,如果 child 在其 parent 的边界之外,它可以是可见的(如果所有祖先都将 clipsToBounds
设置为 false),但永远不会接收到触摸,因为它的 parent 的 pointInside(_:withEvent:)
将拒绝出现在 child 视图上的触摸。
您可以通过使用 Xcode's “Debug View Hierarchy” feature 检查您的视图层次来诊断最后两种情况。
原来 Googlemaps 的 GMSView 在视图中使用了其他手势,必须明确禁止:
mapView.settings.consumesGesturesInView = false;
我正在尝试为我的应用程序弹出一个弹出窗口。到目前为止,弹出窗口在固定坐标处弹出,我试图让它在用户点击的位置弹出。这是我的:
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)
}
}
}
我注意到当我点击模拟器时打印语句从不打印。
我在视图中启用了 interaction
和 multi-touch
。我知道这很好用,因为我还将它与 Google 地图集成在一起,因此当我点击时,会出现一个 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
}
而且我也看到了打印语句。不确定我在这里遗漏了什么。
为超级视图和视图启用了用户交互:
如果 userInteractionEnabled
为真,则应调用 touchesBegan,您的代码将起作用,弹出窗口将出现在用户点击的位置。
如果您的视图是子视图,请确保 userInteractionEnabled 在任何父视图上都为真。查看
视图没有在您期望的时候接收到触摸的主要原因有以下三个:
已将
userInteractionEnabled
设置为 false。 Apparent只是您已经证实情况并非如此。其他一些视图在感兴趣的视图“之上”,并且正在接收触摸。请注意,child 视图位于其 parent 之上(在 child 覆盖的区域中),故事板文档大纲中较低的视图位于任何兄弟姐妹之上在大纲中更高。 (在您的屏幕截图中,KittyMapper® 在地图视图的顶部,在它们重叠的任何区域。)如果您不希望顶部视图接收触摸,则需要将
userInteractionEnabled
设置为 false。感兴趣的视图超出其 parent 的范围。
命中测试以递归方式工作:window 对其每个 children(按 top-to-bottom 顺序)调用
hitTest(_:withEvent:)
,直到一个 returns non-nil;hitTest(_:withEvent:)
returns 如果pointInside(_:withEvent:)
returns false 立即为零;否则hitTest(_:withEvent:)
在 child 上调用hitTest(_:withEvent:)
(按 top-to-bottom 顺序)直到一个 returns non-nil 和 returnsself
如果 children 的 none 报告命中。因此,如果 child 在其 parent 的边界之外,它可以是可见的(如果所有祖先都将
clipsToBounds
设置为 false),但永远不会接收到触摸,因为它的 parent 的pointInside(_:withEvent:)
将拒绝出现在 child 视图上的触摸。
您可以通过使用 Xcode's “Debug View Hierarchy” feature 检查您的视图层次来诊断最后两种情况。
原来 Googlemaps 的 GMSView 在视图中使用了其他手势,必须明确禁止:
mapView.settings.consumesGesturesInView = false;