【问题标题】:Using UILongPressGestureRecognizer together with draggable MKPinAnnotationView in MKMapView在 MKMapView 中使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView
【发布时间】:2012-01-11 15:27:50
【问题描述】:

我在使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView 时遇到问题。

我试图产生的行为类似于地图应用程序。

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

但是,我无法在 MKPinAnnotationView 的框架外识别长按。如果 Pin 不可拖动,则放置 Pin 的长按手势可以正常工作。但是,当 pin 可拖动时,我无法识别长按手势识别器,因此我可以放下 pin。

有什么想法吗?

顺便说一句,我已经尝试为长按识别器设置委托,以便

- (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;

    }

}

【问题讨论】:

    标签: ios mapkit uigesturerecognizer mkannotationview


    【解决方案1】:

    好的,我解决了。

    显然,当我对 MKMapView 进行子类化后,我还添加了一个方法 handleLongPress。这个方法显然干扰了 MKMapView 的 handleLongPress 方法。

    只需将我的 handleLongPress 选择器更改为不同的名称,如 handleLongPress2 即可使其像地图应用一样工作。

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

    【讨论】:

    • 请帮助我,我坚持相同。很好地放下别针,但如何拖动该注释。请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2012-08-09
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多