ios - 拖动注释时的回调

标签 ios drag-and-drop annotations

我可以拖动我的注释,当我放下它时我可以读取位置。但是,我需要不断地用我拖动的位置更新我的标题。我试过添加一个 UIPanGestureRecognizer,但是当我拖动注释时它不起作用。在拖动注释时,我应该使用什么方法来不断回调我的代码?

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
                didChangeDragState:(MKAnnotationViewDragState)newState
                fromOldState:(MKAnnotationViewDragState)oldState {

CLLocationCoordinate2D annoCoord = view.annotation.coordinate;

switch (newState) {
    case MKAnnotationViewDragStateStarting:
        NSLog(@"Start dragging annotation");
        break;

    case MKAnnotationViewDragStateDragging:
        NSLog(@"Dragging annotation");
        break;

    case MKAnnotationViewDragStateEnding:
        [view setDragState:MKAnnotationViewDragStateNone]; // must be here! else multiple calls
        NSLog(@"Ending dragging annotation at %f : %f", annoCoord.latitude, annoCoord.longitude);
        break;

    case MKAnnotationViewDragStateCanceling:
        NSLog(@"Cancel dragging annotation");
        break;

    case MKAnnotationViewDragStateNone:
        NSLog(@"None dragging annotation");
        break;
    default:
        break;
    }

}

我只会在状态开始时收到回调,在拖动期间不会。

如有任何帮助,我们将不胜感激。

干杯,加油

最佳答案

您可以在 MKAnnotationView 中心属性上设置观察者。然后在观察者的回调中,您可以将注释 View 中心位置从屏幕坐标转换为地理坐标。

[myAnnotationView addObserver:myMapViewDelegate forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
...
- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    CGPoint position = myAnnotationView.center;
    //... here take  myAnnotationView.centerOffset into consideration to get the correct coordinate  
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:position toCoordinateFromView:self.superview];
}

关于ios - 拖动注释时的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918210/

相关文章:

java - Spring Security @PreAuthorize 使用 SpEL 语言访问 Autowiring bean

gradle - Groovy @Slf4j 注释 : "Class path contains multiple SLF4J bindings"

枚举值 : passing the annotation instance on to user code 上的 Java 注释

iphone - iOS:无法使用导航 Controller 显示 View

iOS - 由 tableView 附件触发的 Segue?

ios - 使用 XLPagerTabStrip 和 UITableView 时出现 TopPadding

Android:将项目拖出滚动列表/画廊

objective-c - 为什么没有对应ivar 的子类@property 会隐藏父类(super class)ivar?

drag-and-drop - Kendo UI DragAndDrop TreeView 项目到 ListView

javascript - 增强 Jquery 拖放演示