2013-08-19 26 views
4

我有一個自定義MKAnnotationView,我從XIB加載。當我第一次加載mapview時,我有幾個標準MKAnnotationView自定義MKAnnotationView間歇性識別觸摸

當用戶選擇一個時,顯示自定義MKAnnotationView。我希望用戶能夠點擊自定義註釋視圖內的任何位置以呈現新的視圖控制器。

我已經試過(所有這些都建議我發現這裏StackOverflow上):

奇怪的是,如果我在註解存在時拖動地圖,按鈕就可以正常工作。該問題僅在我首次顯示自定義視圖時顯示。

任何想法,將不勝感激。

+0

你能告訴什麼用第一時恰好不工作方案? – haawa

回答

0

你應該檢出MKMapKit委託文件,它有很多很好的方法可以用來完成你想要做的事情。我會絕對不嘗試並添加一個UIButton到註釋視圖。

管理註釋視圖 - MapView類:viewForAnnotation: - MapView類:didAddAnnotationViews: - MapView類:annotationView:calloutAccessoryControlTapped: 拖動註釋視圖 - MapView類:annotationView:didChangeDragState:fromOldState: 選擇註釋視圖 - MapView的:didSelectAnnotationView:
- MapView類:didDeselectAnnotationView:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
if([annotation isKindOfClass: [MKUserLocation class]]) 
{ 
    return nil; 
} 
else if([annotation isKindOfClass:[MYCLASS class]]) 
{ 
    static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; 
    MKAnnotationView *annotationView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; 
    if (annotationView == nil) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier]; 
    } 

    //Add an Image! 
    annotationView.image = [UIImage imageNamed:@"trashMarker.png"]; 

    //Move the Frame Around! 
    [annotationView setFrame:CGRectMake(annotationView.frame.origin.x, annotationView.frame.origin.y - annotationView.frame.size.height, annotationView.frame.size.height, annotationView.frame.size.width)]; 

    //Finally Set it as the annotation! 
    annotationView.annotation = annotation; 

    //Return the annotationView so the MKMapKit can display it! 
    return annotationView; 
}} 

你的MKAnnotation(IE符合協議)的子類默認應該包含一個標籤(我認爲),但是如果它不只是你自己添加一個屬性,所以你可以區分地圖上的不同標記。你的方法應該是這個樣子,

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
MYCLASS *selectedMapPin = view.annotation; 
if(selectedMapPin.tag == MY_PIN_TAG) 
{ 
    //SHOW VIEW CONTROLLER 
}} 

更多的例子可以參考我們的開源項目綠起來佛蒙特州

Green Up Vermont iOS App