ios - 在 MKMapView 中使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView

标签 ios mapkit uigesturerecognizer mkannotationview

我在将 UILongPressGestureRecognizer 与可拖动的 MKPinAnnotationView 一起使用时遇到问题。

我尝试产生的行为类似于 map 应用程序。

  1. 图钉可以拖动。
  2. 长按/点击时,会掉落一个图钉。

但是,我在 MKPinAnnotationView 框架外识别长按时遇到了问题。如果 Pin 不可拖动,长按手势放下 Pin 效果很好。但是,当图钉可拖动时,我无法识别长按手势识别器,因此我无法放下图钉。

有什么想法吗?

顺便说一下,我已经尝试为长按识别器设置委托(delegate)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

在这种情况下,长按手势被识别并且图钉被放下,但是图钉的拖动不再起作用。

MapView 的片段(MKMapView 的子类)

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

    // init the gesture recognizer
        UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc] 
            initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 0.5f; //user needs to press for 2 seconds
        lpgr.delegate = self;
        [self addGestureRecognizer:lpgr];
        [lpgr release];

        //add some initial annotation
        Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
        [_annotation titleWithString:@"some title"];
        [self addAnnotation:_annotation];

    }

    return self;

}

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
    {
        return;
    }

    CGPoint touchPoint = [gestureRecognizer locationInView:self];   
    CLLocationCoordinate2D touchMapCoordinate = [self convertPoint:touchPoint toCoordinateFromView:self];

    // add marker to self-map
    // Marker is subclass of MKAnnotation
    Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
    [_annotation titleWithString:@"some title"];
    [self addAnnotation:_annotation];

}


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

    if([annotation isMemberOfClass:[Marker class]] ) {

        // use MKPinAnnotationView for the view
        MKPinAnnotationView *_pin = (MKPinAnnotationView *) [mView dequeueReusableAnnotationViewWithIdentifier:@"spot_pin"];

        if (_pin == nil)
        {
            _pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot_pin"] autorelease];
        }
        else
        {
            _pin.annotation = annotation;
        }

        [_pin setDraggable:YES];
        [_pin setSelected:YES animated:YES];
        [_pin setCanShowCallout:YES];

        return _pin;

    } else {

        return nil;

    }

}

最佳答案

好的,我解决了。

显然,在我继承 MKMapView 之后,我还添加了一个方法 handleLongPress。此方法显然干扰了 MKMapView 的 handleLongPress 方法。

只需将我的 handleLongPress 选择器更改为不同的名称(例如 handleLongPress2),它就会像 map 应用一样工作。

    UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self action:@selector(handleLongPress2:)];

关于ios - 在 MKMapView 中使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821872/

相关文章:

ios - Instrument 释放后仍报内存泄漏

iphone - 如何在没有放置动画的情况下自动在 map 注释图钉上显示气泡

ios - Swift - 如果在 View 外部触摸则隐藏 View

javascript - 检测 IOS 中是否存在应用程序,否则重定向到应用程序商店 Javascript

ios - Swift:为 CustomTab 重用一个 ViewController

ios - Cocoapods循环依赖问题

swift - 根据注释的属性设置注释的字形文本

ios - 如何重新定位 MKMapView 的指南针?

objective-c - Objective-C : Gesture Issue

ios - 双击 uitextview