2012-01-11 57 views
3

我在使用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; 

    } 

} 

回答

-1

好球員,我解決了這個問題。

顯然,當我繼承了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

相關問題