MKAnnotation在长按可拖动之前需要单击

bob*_*lls 2 mapkit mkmapview mkannotation mkannotationview ios

我已经阅读了很多关于MKAnnotation你需要setCoordinate在子类中实现的内容,以及draggable=TRUE为了使整个shebang可拖动.

我的情况是,在我的iOS7专用应用程序中,无论我是否实现,我的注释都是可拖动的setCoordinate...但问题是我需要先点击它(弹出标注附件)然后长按它,并且只然后它会悬停在地图上方的空中并可以拖动.这对用户来说很困惑,因为它与标准地图应用程序中的方式不同.请注意,在Google地图应用中,长按一个注释会使其悬停和拖动而不需要先决条件.

我已经尝试过实施setCoordinate,但这没有任何区别.除此之外,我的注释子类只存储纬度和经度,这很好.我只是希望它能够在长时间内直接拖拽.

实现的View Controller的相关代码MKMapViewDelegate.我可以通过在委托方法中放置断点来验证这一点.

- (void)viewDidLoad
{
        [super viewDidLoad];
[mapView setDelegate:self];    
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {

    MKPinAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *defaultPinID = @"pointPin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
        }
        pinView.pinColor = MKPinAnnotationColorRed;
        if ([annotation isKindOfClass:[SimpleMapAnnotation class]]) {
            SimpleMapAnnotation *simpleMapAnnotation = (SimpleMapAnnotation*)annotation;
            if ([simpleMapAnnotation color]) {
                pinView.pinColor = [simpleMapAnnotation color];
            }
            if (simpleMapAnnotation.moveable) {
                pinView.draggable=TRUE;
                // delete button to remove an annotation
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"trash" ofType:@"png"]] forState:UIControlStateNormal] ;
                button.frame = CGRectMake(0, 0, 23, 23);
                pinView.rightCalloutAccessoryView = button;
            }
        }
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    }
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

- (void)mapView:(MKMapView *)theMapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control{
    if([view.annotation isKindOfClass:[SimpleMapAnnotation class]]){
        SimpleMapAnnotation *annotation = (SimpleMapAnnotation*)view.annotation;

        //remove the point from the database
    //<snip>

        [UIView animateWithDuration:0.3 delay:0.0 options:0 animations:(void (^)(void)) ^{
            //remove the annotation from the map
            view.alpha = 0.0f;
        }
                         completion:^(BOOL finished){
                             [theMapView removeAnnotation:annotation];
                             view.alpha=1.0f;
                         }];

    }
}


- (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
   fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        if ([annotationView.annotation isMemberOfClass:[SimpleMapAnnotation class]]) {
            SimpleMapAnnotation *simpleMapAnnotation = (SimpleMapAnnotation*)annotationView.annotation;
            simpleMapAnnotation.latitude = [NSNumber numberWithDouble:simpleMapAnnotation.coordinate.latitude];
            simpleMapAnnotation.longitude = [NSNumber numberWithDouble:simpleMapAnnotation.coordinate.longitude];
        }
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}
Run Code Online (Sandbox Code Playgroud)

Iva*_*lev 5

要开始拖动MKAnnotationView对象,应首先选择它.这是设计的.

如果要在长按时立即开始移动注释视图,则应将该selected属性设置为YES在将该长按钮的触摸传递到对象之前.

要做到这一点,使MKPinAnnotationView类的继承者如下:

// MKImmideateDragPinAnnotationView.h
@interface MKImmideateDragPinAnnotationView : MKPinAnnotationView
@end


// MKImmideateDragPinAnnotationView.m
@implementation MKImmideateDragPinAnnotationView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setSelected:YES];
    [super touchesBegan:touches withEvent:event];
}

@end
Run Code Online (Sandbox Code Playgroud)

然后在代码中将分配更改MKPinAnnotationViewMKImmideateDragPinAnnotationViewpinView:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    ...

    if ( pinView == nil ) {
        pinView = [[MKImmideateDragPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

您的引脚将在长按时立即开始拖动.并像往常一样在单击时显示标注.

这个技巧适用MKAnnotationView于iOS 6.xx中的任何类 - iOS 7.xx.