2012-04-02 96 views
1

添加手勢在我的iPhone應用程序我想允許用戶點按標註的泡沫帶他去另一個視圖 我怎樣才能添加姿態向泡沫我怎麼能在CallOutBubble

我試圖把姿態在MKAnnotationView當用戶選擇註釋

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showItem:)]; 
    tapped.numberOfTapsRequired = 1; 
    view.userInteractionEnabled = YES; 
    [view addGestureRecognizer:tapped]; 
} 

但這並不爲泡沫 任何的工作有所幫助?

+0

推薦的行爲是添加一個正確的配件視圖,我建議你一開始這樣做。用戶更容易找到他(她)可以點擊的地方。有用。 – 2012-04-02 15:46:46

回答

1
  1. 您已經在註解視圖(引腳)上設置了手勢識別器,而不是作爲單獨視圖的標註泡泡。最重要的是,由於與地圖識別器的衝突,我不確定識別器是否可以工作。

  2. 作爲@CyrilGodefroy評論,你所做的是比標準的方式把控制放在標註泡沫中的用戶更難以識別。該框架提供用於此目的的標註附件的觀點:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
        // Add a detail disclosure button to the callout. 
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
        [rightButton addTarget:self action:@selector(myShowDetailsMethod:) forControlEvents:UIControlEventTouchUpInside]; 
        view.rightCalloutAccessoryView = rightButton; 
    } 
    
+0

令人沮喪的是,當您將控件添加到標註時,標註突然變得可選(意味着我可以點擊它並突出顯示),但您無法在控件之外(但在標註泡泡上)檢測到此選擇。這對用戶來說是很混亂的,因爲他們必須精確地點擊按鈕才能得到一個動作,但要點擊標註泡泡突出顯示,這通常是「選擇和操作」的信號。 AppleMaps在點擊標註或控制器時具有相同的行爲,但我一直無法弄清楚如何複製 – MoMo 2015-09-09 20:17:41

0

的期望的行爲是能夠在標註氣泡的任何地方挖掘和已對其執行在rightCalloutAccessory按鈕的動作。

它是需要的,因爲加入UIControl要麼calloutAccessory使得整個標註泡沫可選(意味着當你點擊它,錯誤地指示給用戶,這應該產生一個動作它突出),但不提供實施者任何方式檢測並響應此抽頭,這導致了令人困惑的用戶體驗。

下面是我解決問題的方法。

當創建MKAnnotationView,一個tapGesture添加到MKAnnotationView

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation.reuseID]; 
aView.canShowCallout = YES; 

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:annotation.callOutImageName]]; 
    aView.leftCalloutAccessoryView = image; 

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [obsDetailButton setImage:[UIImage imageNamed:@"your_image.png"] forState:UIControlStateNormal]; 
    aView.rightCalloutAccessoryView = detailButton; 

    UITapGestureRecognizer *singleTapOnCallout = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)]; 
    [aView addGestureRecognizer:singleTapOnCallout]; 

} 

然後,在回調方法用於姿勢,如果選擇了annotationView檢查,並且如果是這樣,則執行所需的操作

- (void)calloutTapped:(UIGestureRecognizer *)sender { 

    MKAnnotationView *annotationView = (MKAnnotationView *)sender.view; 

    if (annotationView.selected) { 
     //Perform desired action on selection 
    } 
} 

現在這不是一個「完美」的解決方案,因爲如果您點擊註釋一次,顯示標註,然後在顯示標註時點擊標註(不是標註),則會執行操作。不過,我認爲這是一個更直觀的體驗,而不是讓您的標註突出顯示而沒有發生任何行動,所以在給出選擇的情況下,我更喜歡這一點。

很明顯,理想的解決方案是讓Apple公開默認的標註視圖並允許您直接爲其添加一個gestureRecognizer。或者甚至更好,因爲它提供了一個委託方法,告訴你插入的標註。

在這種情況發生之前,這是我能想出的最佳解決方案。希望它能幫助別人!