简体   繁体   English

如何在 MKMapView 上禁用用户交互?

[英]How to disable user interaction on MKMapView?

How do you disable user interaction to an entire Map (MKMapView) in iOS?如何在 iOS 中禁用用户与整个地图 (MKMapView) 的交互? I just want to disable zooming, tapping, etc and show a static map.我只想禁用缩放、点击等功能并显示静态地图。 [self.mapView setUserInteractionEnabled:NO] didn't work. [self.mapView setUserInteractionEnabled:NO]不起作用。 Found no working solutions via Google or other answers here on Stack (pls link if you find a working answer..) .通过谷歌或 Stack 上的其他答案没有找到有效的解决方案(如果你找到了有效的答案,请链接......) Targeting iOS 5. There should be an easier way to do this.针对 iOS 5。应该有一种更简单的方法来做到这一点。

The key is to disable zooms and scrolls.关键是禁用缩放和滚动。

In Objective-C:在 Objective-C 中:

self.mapView.zoomEnabled = false;
self.mapView.scrollEnabled = false;
self.mapView.userInteractionEnabled = false;

Or Swift:或斯威夫特:

mapView.isZoomEnabled = false
mapView.isScrollEnabled = false
mapView.isUserInteractionEnabled = false

By the way, if you want a static map, you might consider using MKMapSnapshotter instead.顺便说一下,如果你想要一个静态地图,你可以考虑使用MKMapSnapshotter This creates an image representation of a map.这将创建地图的图像表示。 If you have annotations or overlays, you have to render them manually, but, depending upon your use-case, map snapshots might do the job.如果您有注释或叠加层,则必须手动渲染它们,但根据您的用例,地图快照可能会完成这项工作。

You can do this in Interface Builder like this:您可以在 Interface Builder 中执行此操作,如下所示:

像这样

How about this little hack!这个小黑客怎么样! just place UIView on top of the MKMapView having all the same frame details.只需将UIView放在具有所有相同框架细节的MKMapView之上。 Also note that the backgroundColor of this view should be clearColor另请注意,此视图的backgroundColor应为clearColor

You can set the enabled property of MKAnnotationView to NO您可以将 MKAnnotationView 的 enabled 属性设置为 NO

For disabling Zoom, you can set mapView.zoomEnabled = FALSE;要禁用缩放,您可以设置mapView.zoomEnabled = FALSE;

enabled启用

A Boolean value indicating whether the annotation is enabled.一个布尔值,指示是否启用注释。

@property (nonatomic, getter=isEnabled) BOOL enabled

Discussion讨论

The default value of this property is YES.此属性的默认值为 YES。 If the value of this property is NO, the annotation view ignores touch events and cannot be selected.如果该属性的值为NO,则注释视图将忽略触摸事件并且无法选择。 Subclasses may also display the annotation contents differently depending on the value of this property.子类还可以根据此属性的值不同地显示注释内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM