Ios MKMapView上的PointAnnotation标注出现,然后立即消失
我正在创建一个简单的点批注,其中包含Ios MKMapView上的PointAnnotation标注出现,然后立即消失,ios,swift,annotations,mkmapview,callout,Ios,Swift,Annotations,Mkmapview,Callout,我正在创建一个简单的点批注,其中包含UITapGestureRecognizer委托内的标注 我第一次点击地图时,图钉与标注一起出现,但标注随后立即消失 我第二次点击同一个pin时,标注会出现并保持在那里,不知道为什么它会在第一次消失 @IBAction func handleMapTouch(recognizer: UITapGestureRecognizer){ let view = recognizer.view let touchPoint=recognizer.loca
UITapGestureRecognizer
委托内的标注
我第一次点击地图时,图钉与标注一起出现,但标注随后立即消失
我第二次点击同一个pin时,标注会出现并保持在那里,不知道为什么它会在第一次消失
@IBAction func handleMapTouch(recognizer: UITapGestureRecognizer){
let view = recognizer.view
let touchPoint=recognizer.locationInView(view)
var touchCord=CLLocationCoordinate2D()
touchCord = mapView.convertPoint(touchPoint, toCoordinateFromView:
mapView)
mapView.removeAnnotations(mapView.annotations)
pointAnnotation.coordinate=touchCord
pointAnnotation.title="ABC"
pointAnnotation.subtitle="DEF"
mapView.addAnnotation(pointAnnotation)
mapView.selectAnnotation(pointAnnotation, animated: true)
}
我也有同样的问题。我也不知道如何解决这个问题,但我找到了一个解决办法。也许它也能帮助你 我使用了
longpress手势
来替换tappostate
在视图中加载:
let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)
if(gestureRecognizer.state == .Ended){
self.mapView.removeGestureRecognizer(gestureRecognizer)
//remove all annotation on the map
self.mapView.removeAnnotations(self.mapView.annotations)
//convert point user tapped to coorinate
let touchPoint: CGPoint! = gestureRecognizer.locationInView(self.mapView)
let touchMapCoordinate: CLLocationCoordinate2D = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
showCustomAnnotation(touchMapCoordinate)
}
self.mapView.addGestureRecognizer(gestureRecognizer)
在函数addAnnotation中:
let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)
if(gestureRecognizer.state == .Ended){
self.mapView.removeGestureRecognizer(gestureRecognizer)
//remove all annotation on the map
self.mapView.removeAnnotations(self.mapView.annotations)
//convert point user tapped to coorinate
let touchPoint: CGPoint! = gestureRecognizer.locationInView(self.mapView)
let touchMapCoordinate: CLLocationCoordinate2D = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
showCustomAnnotation(touchMapCoordinate)
}
self.mapView.addGestureRecognizer(gestureRecognizer)
以防万一,其他人也有同样的问题,虽然基思的答案是有效的,但在我的例子中,它会干扰与地图相关的其他手势,如收缩和缩放 对我来说,延迟几毫秒显示标注的操作效果更好 在Swift 3中:
let deadlineTime=DispatchTime.now()+毫秒(500)
DispatchQueue.main.asyncAfter(截止日期:deadlineTime){
mapView.addAnnotation(pointAnnotation)
mapView.selectAnnotation(pointAnnotation,动画:true)
}
尝试删除这条线-地图视图。选择Annotation(pointAnnotation,动画:true)这是我第一次编码它的方式,但是没有这条线,调用在第一次单击时根本不会显示(即使是很短的时间)。这个问题仍然没有解决,有人能帮忙吗?