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

时间:2012-01-11 15:27:50

标签: ios mapkit uigesturerecognizer mkannotationview

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

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

  1. 可以拖动别针。
  2. 长按/敲击时,针脚掉落。
  3. 但是,我在MKPinAnnotationView框架外识别长按时遇到问题。如果引脚不可拖动,则按下引脚的长按手势可以正常工作。然而,当引脚可以拖动时,我无法识别长按手势识别器,以便我可以放下引脚。

    有什么想法吗?

    顺便说一下,我试图设置长按识别器的代理,以便

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

1 个答案:

答案 0 :(得分:-1)

好的,我解决了。

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

只需将handleLongPress选择器更改为其他名称(如handleLongPress2),就可以使其像地图应用一样工作。

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