如何在 MKMapView 上捕获点击手势
我正在尝试捕获 MKMapView
上的点击事件,这样我可以在用户点击的点上放置 MKPinAnnotation
。基本上,我有一个覆盖有 MKOverlayViews
(显示建筑物的覆盖层)的地图,当用户通过放置 MKPinAnnotaion
点击该覆盖层时,我想向用户提供有关该覆盖层的更多信息并在标注中显示更多信息。 谢谢。
I am trying to capture tap event on my MKMapView
, this way I can drop a MKPinAnnotation
on the point where user tapped. Basically I have a map overlayed with MKOverlayViews
(an overlay showing a building) and I would like to give user more information about that Overlay when they tap on it by dropping a MKPinAnnotaion
and showing more information in the callout.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
UIGestureRecognizer
来检测地图视图上的触摸。不过,我建议不要单击一下,而是双击 (
UITapGestureRecognizer
) 或长按 (UILongPressGestureRecognizer
)。单击可能会干扰用户尝试单击图钉或标注本身。在设置地图视图的位置(例如在
viewDidLoad
中),将手势识别器附加到地图视图:或使用长按:
在
handleGesture:
方法中:You can use a
UIGestureRecognizer
to detect touches on the map view.Instead of a single tap, however, I would suggest looking for a double tap (
UITapGestureRecognizer
) or a long press (UILongPressGestureRecognizer
). A single tap might interfere with the user trying to single tap on the pin or callout itself.In the place where you setup the map view (in
viewDidLoad
for example), attach the gesture recognizer to the map view:or to use a long press:
In the
handleGesture:
method:我在
viewDidLoad:
中设置了长按 (UILongPressGestureRecognizer
),但它只检测到第一次触摸。在哪里可以设置长按来检测所有触摸?(这意味着每次等待用户触摸屏幕按下图钉时地图就准备好)
viewDidLoad:
方法!和
handleLongPressGesture
方法:I setup a long press (
UILongPressGestureRecognizer
) inviewDidLoad:
but it just detect the only one touch from the first.Where can i setup a long press to detect all touch? (it means the map ready everytime waiting user touch to screen to push a pin)
The
viewDidLoad:
method!and the
handleLongPressGesture
method:如果您想在地图视图中使用单击/点击,这里是我正在使用的代码片段。 (Cocoa 和 Swift)
在手势委托方法中,这是一个更喜欢双击手势的简单测试……
如果您希望地图处于各种“状态”,您还可以在其他委托方法中过滤手势,其中之一允许单击/点击
If you want to use a single click/tap in the map view, here's a snippet of code I'm using. (Cocoa and Swift)
in the gesture delegate method, a simple test to prefer the double-tap gesture …
you could also filter the gesture in other delegate methods if you wanted the Map to be in various "states", one of which allowed the single tap/click
由于某种原因,UIGestureRecognizer 在 Swift 中对我不起作用。当我使用 UIGestureRecognizer 方式时。当我使用touchesEnded方法时,它返回一个MKNewAnnotationContainerView。看来这个 MKNewAnnotationContainerView 挡住了我的 MKMapView。幸运的是,它是 MKMapView 的子视图。所以我循环遍历 MKNewAnnotationContainerView 的超级视图直到 self.view 来获取 MKMapView。我成功地通过点击固定了地图视图。
斯威夫特4.1
For some reason, the UIGestureRecognizer just didn't work for me in Swift. When I use the UIGestureRecognizer way. When I used the touchesEnded method, it returns a MKNewAnnotationContainerView. It seems that this MKNewAnnotationContainerView blocked my MKMapView. Fortunately enough, it's a subview of MKMapView. So I looped through MKNewAnnotationContainerView's superviews till self.view to get the MKMapView. And I managed to pin the mapView by tapping.
Swift 4.1