我正在创建一个简单的点注释,在UITapGestureRecognizer委托中有一个标注。
当我第一次点击地图时,图钉与标注一起出现,但标注在那之后立即消失。
第二次点击相同的引脚时,标注出现并停留在那里,不确定为什么第一次它消失了。
@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)
}发布于 2017-01-12 22:49:39
以防其他人也有同样的问题,尽管Keith的答案起作用了,但在我的例子中,它扰乱了与地图相关的其他手势,如收缩和缩放。
对我来说,延迟几毫秒显示标注的动作效果更好。
在Swift 3中:
let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
mapView.addAnnotation(pointAnnotation)
mapView.selectAnnotation(pointAnnotation, animated: true)
}发布于 2016-03-11 14:26:40
我也有同样的问题。我也不知道如何解决这个问题,但我找到了一个变通办法。也许它也能帮到你。
我用LongPressGesture代替了TapGesture
视图中的加载:
let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)函数addAnnotation中的:
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)https://stackoverflow.com/questions/32137993
复制相似问题