Ios 在移动时跟踪并拖动?
在拖动过程中移动MKAnnotation时,是否可以跟踪其坐标更改?我遵循了管理视图拖动状态的方法,但是如果我放入一些打印,似乎mapViewmapView、view、newState、oldState中的拖动状态只在拖动开始时发生一次。然后当我释放时,我得到了结局。但在移动过程中没有实时更新Ios 在移动时跟踪并拖动?,ios,mapkit,mkannotation,mkannotationview,Ios,Mapkit,Mkannotation,Mkannotationview,在拖动过程中移动MKAnnotation时,是否可以跟踪其坐标更改?我遵循了管理视图拖动状态的方法,但是如果我放入一些打印,似乎mapViewmapView、view、newState、oldState中的拖动状态只在拖动开始时发生一次。然后当我释放时,我得到了结局。但在移动过程中没有实时更新 我可以启动一个计时器,在拖动过程中轮询注释,但我希望能少一点麻烦。抱歉,聚会迟到了。我也有同样的需要,遇到了你的职位。这就是我所做的 首先是注释方法的地图视图代理视图。注释视图可拖动,并添加了手势识别器。
我可以启动一个计时器,在拖动过程中轮询注释,但我希望能少一点麻烦。抱歉,聚会迟到了。我也有同样的需要,遇到了你的职位。这就是我所做的 首先是注释方法的地图视图代理视图。注释视图可拖动,并添加了手势识别器。My DetailAnnotationView是MKAnnotationView的子类。它给党带来的东西与我们的关注点无关
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = overviewMap.dequeueReusableAnnotationView(withIdentifier: DetailAnnotationView.reuseIdentifier) as? DetailAnnotationView
if annotationView == nil {
annotationView = DetailAnnotationView(annotation: nil)
annotationView!.isDraggable = true
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureHandler))
recognizer.allowableMovement = CGFloat.infinity
recognizer.delegate = self
annotationView!.addGestureRecognizer(recognizer)
}
annotationView!.updateBounds(self)
return annotationView
}
接下来是手势处理程序。detailAnnotation是一个MKPointAnnotation。请注意,我正在使用MKMapView.convert的结果重新定位地图。当然,你可以随心所欲地使用它
@objc func longPressGestureHandler(_ recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .changed:
guard let annotationView = overviewMap.view(for: detailAnnotation) else { fatalError("Cannot get detail annotation's view") }
detailMap.region.center = overviewMap.convert(recognizer.location(in: annotationView), toCoordinateFrom: annotationView)
default: break
}
}
最后,我允许我的手势识别器与MapKit用于拖动注释视图的手势识别器同时操作
extension DualMapsManager : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer is UILongPressGestureRecognizer && gestureRecognizer.delegate === self && otherGestureRecognizer is UILongPressGestureRecognizer
}
}
就在这里。如果你结束了投票,那么你现在有了另一个选择。如果您找到了一个不同的解决方案,那么为什么不发布它呢?我最终确实完成了轮询机制的工作。三种方法
// helper method to trigger a redraw of the renderer associated with a given overlay
// despite the documentations claim to otherwise, it seems that the parameterized
// version of setNeedsDisplay does a better job of updating caches
func redrawOverlay(_ overlay:RegionOverlay?) {
guard let overlay = overlay, let renderer = self.mapView.renderer(for: overlay) else { return }
renderer.setNeedsDisplay(self.mapView.visibleMapRect)
}
// the "worker" method that is called once we enter .dragging state and then
// repeatedly calls myself as long as that .dragging state stays in effect
func updateMapWhileDragging(_ annotationView:MKAnnotationView, first:Bool = false) {
if annotationView.dragState == .dragging {
let point = annotationView.bounds.midMid
let coordinate = self.mapView.convert(point, toCoordinateFrom: annotationView)
if let annotation = annotationView.annotation as? RegionEditingAnnotation {
if first {
annotation.beginMove(at: coordinate)
}
else {
annotation.move(to: coordinate)
}
if let overlay = self.findOverlay(region: annotation.region) {
overlay.region = annotation.region
self.redrawOverlay(overlay)
}
}
// ding with this method again in 40ms
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(40)) {
self.updateMapWhileDragging(annotationView)
}
}
}
// the MK delegate method where we can handle most of the states with
// boilerplate responses
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationView.DragState, fromOldState oldState: MKAnnotationView.DragState) {
self.shouldZoom = false
switch newState {
case .starting:
view.setDragState(.dragging, animated: true)
case .dragging:
self.updateMapWhileDragging(view, first: true)
case .ending:
if let annotation = view.annotation as? RegionEditingAnnotation, let mappable = self.selectedOverlay?.mappable {
annotation.endMoves(at: annotation.coordinate)
self.selectedOverlay?.region = annotation.region
self.redrawOverlay(self.selectedOverlay)
mappable.regionPush(annotation.region)
view.setDragState(.none, animated: true)
// call the didSet function directly, because the compiler's too "smart" to reset the same value
self.selectedOverlay_didSet(from: self.selectedOverlay)
}
case .canceling:
view.setDragState(.none, animated: true)
case .none:
break
@unknown default:
break
}
}
下面是它最后的样子
当/如果我回到这里,我会尝试一下@Verticon的答案