2014-12-02 70 views
2

我有我在哪裏執行以下操作註釋的地圖:檢查是否用戶已經選擇了另一個註釋在地圖

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{ 
[self methodA];} 


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
[self methodB];} 

兩種方法A和B刪除或mapView.superview添加視圖。

當處理一個註釋時,所有的工作都很好。 問題是當我有多個註釋,並且我選擇了兩個,一個接一個的時候,另一個是

如果我點擊地圖中的任何其他地方,一切正常。但是當我在第一個註釋視圖之後點擊第二個註釋視圖時,它執行「didDeselectAnnotationView」,然後是「didSelectAnnotationView」,它同時調用方法A和B,但這不是我想要的。我希望它能檢測到我點擊了另一個註釋並忽略了這兩種方法。

我已經研究過這個,但還沒有找到任何解決方案。

我嘗試添加一個全局變量並使用它,以及識別用戶觸摸:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

UITouch *touch = [touches anyObject]; 

// Get the specific point that was touched 
CGPoint point = [touch locationInView:self.mapView]; 

for (id<MKAnnotation> annotation in self.mapView.annotations) { 
    MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation]; 
    if (anView){ 

     CGRect frame = CGRectMake(anView.frame.origin.x-5, anView.frame.origin.y-5, anView.frame.size.width+5, anView.frame.size.height+5); 
     if (CGRectContainsPoint(frame, point)) { 

      NSLog(@"BELONGS"); 
     } 

    } 

}} 

然而,這並沒有捕捉到所有的接觸,再加上它是一個有點意大利麪條解決方案。

任何想法如何解決這個問題?

所有最優秀的

+0

可能類似:http://stackoverflow.com/questions/23667606/detect-when-a-second-annotation-is-selected-in- a-mkmapview – Anna 2014-12-02 11:41:24

+0

接受的答案使用[self performSelector:@selector(checkShouldHideBottomView :) withObject:view afterDelay:0.5]; 我認爲這不適合做。 – jonypz 2014-12-02 19:58:36

回答

0

委託不這樣做,你不選擇,然後選擇另一註釋視圖和的情況下,你選擇一個新的註解視圖,而已經選擇了一個外殼之間的任何區別。在這兩種情況下,這些調用的順序進行:

  • didDeselect廠景
  • didSelect視圖2

你想兩件事情:

1)不叫的methodB當視圖2的選擇非常接近(及時)取消選擇觀點1。

2)當選擇關閉(在時間上)到先前的選擇時,不調用方法A.

要解決這兩個問題,可以存儲兩個時間戳:1)上次選擇2)上次取消選擇。有了這兩個變量,您可以設置一個閾值來定義上面兩條規則中「close」的含義。

現在,您有一個條件可以防止在快速取消選擇和選擇的特定情況下(這是您從一個選擇切換到另一個時委託實際發生的情況)調用methodA和methodB。

+0

我相信時間戳不會解決我的問題。 用戶應該能夠從註釋點擊註釋到沒有任何外部行爲(添加或刪除其他視圖 - > methodA和methodB) – jonypz 2014-12-02 08:40:04

+0

我更新了我的答案。現在應該清楚了。 – ffarquet 2014-12-02 13:45:45

+0

只有當我將閾值設置爲無窮大時,這纔會起作用,並且當用戶在註釋外單擊時應重置此值。這仍然是一個粗略的解決方案。 – jonypz 2014-12-02 20:03:27

0
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView 
{ 
    indexPathTag=aView.tag; 
    [mapView deselectAnnotation:aView.annotation animated:YES]; 

} 
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView 
{ 
} 

從didSelectAnnotationView調用deselectAnnotation。

+0

不起作用。同樣的行爲 – jonypz 2014-12-02 09:23:43

0

這是我目前的解決方案。儘管我不喜歡它,但現在它仍然有效。 如果你有更好的,請發表一個答案,我會將其標記爲正確的。

我正在使用全局變量來跟蹤當前選定的註釋。我將它初始化爲零。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{ 

[self performSelector:@selector(numberOfSelectedAnnotations) 
      withObject:view.annotation afterDelay:.5]; 
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
     if (self.selectedAnnotation == nil) { 
     [self methodB]; 
     } 
    self.selectedAnnotation = (MKAnnotationView*)view; 
} 


-(void)numberOfSelectedAnnotations{ 
if (self.mapView.selectedAnnotations.count == 0) { 
    [self methodA]; 
    self.selectedAnnotation = nil; 
    } 
} 
-1

這是我的優選解決方案:

private var selectedAnnotationView: MKAnnotationView? 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if let _ = annotation as? MKUserLocation { 
     return nil 
    } 
    let ident = "MyAnnotation" 
    let pin = mapView.dequeueReusableAnnotationViewWithIdentifier(ident) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: ident) 
    pin.animatesDrop = true 
    pin.canShowCallout = true 
    pin.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "annotationTapped:")) 
    pin.rightCalloutAccessoryView = UIButton(type: .InfoDark) 
    return pin 
} 

// This will get called before didDeselectAnnotationView below 
func annotationTapped(tapGesture: UITapGestureRecognizer) { 
    if let annotationView = tapGesture.view as? MKAnnotationView { 

     if annotationView === self.selectedAnnotationView { 
      // same as last time 
     } else { 
      // different annotation view 
     } 

     self.selectedAnnotationView = annotationView 

     // any other logic for handling annotation view tap 
    } 
} 

func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) { 
    if view === self.selectedAnnotationView { 

     // The user did not select another annotation view 
     // (e.g. they tapped the map)    

     self.selectedAnnotationView = nil 
    } 
} 
相關問題