在swift中长按时将引脚注释添加到地图视图中 [英] Adding a pin annotation to a map view on a long press in swift
问题描述
我正在尝试制作一款iPhone应用程序,要求用户能够长时间按下地图视图上的某个地方,以便在那里放置别针。有人知道这是怎么做的吗?
I'm trying to make an iPhone app which requires users to be able to long press on a place on a map view to drop a pin there. Does anybody know how this is done?
当你长按屏幕时,可以在苹果地图中观察到这种行为。它将删除一个引脚并提供一个注释,说drop pin
The behaviour is observable in apple maps when you long press on the screen. It will drop a pin and present an annotation saying "dropped pin"
推荐答案
1)实例化一个 UILongPressGestureRecognizer
并将其添加到 MKMapView
。
1) Instantiate a UILongPressGestureRecognizer
and add it to the MKMapView
.
2)当用户长按后调用选择器时,在 MKMapView <中调用 addAnnotation方法 / code>具有适当的标题和坐标。
2) When the selector gets called after the user has a long press, call the addAnnotation method in MKMapView
with the appropriate title and coordinate.
3)然后确保符合 MKMapViewDelegate
并实现 viewForAnnotation:
将在您添加注释后立即调用并返回 MKPinAnnotationView
3) Then make sure you conform to the MKMapViewDelegate
and implement viewForAnnotation:
which will be called right after you add the annotation and return a MKPinAnnotationView
这篇关于在swift中长按时将引脚注释添加到地图视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!