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

问题描述:

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

我试图制作的行为与地图应用类似。

  1. 该引脚可以被拖动。
  2. 当长按/轻敲时,销钉被丢弃。

但是,我有问题,在MKPinAnnotationView的框架之外识别长按。如果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; 

    } 

} 

好球员,我解决了这个问题。

显然,当我继承了MKMapView之后,我还添加了handleLongPress方法。这种方法显然干扰了MKMapView的handleLongPress方法。

只要将handleLongPress选择器更改为像handleLongPress2这样的不同名称,就可以使其像Google地图应用一样工作。

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

请帮助我,我用同样的方法。很好地放下引脚,但如何拖动该注释。请将hellp我 – user100 2014-07-27 19:29:12