2013-10-18 47 views
1

我想我所有的觀點是可點擊並不僅僅是leftCalloutAccessoryView或r ightCalloutAccessoryView,我也希望中心成爲可點擊以及調出註釋的觀點是不可點擊的中心

enter image description here

+0

我貼我的回答如下。我沒有' t做了一段時間,所以請lemme知道是否有什麼不明確 – abbood

+0

你試過我的答案?我花了一段時間才把它放在一起..如果你至少告訴我它是否與你一起工作,我會很感激的。除非如果希望找到一個由幾行 – abbood

回答

2

您可以使用,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 

    CGPoint touchPoint = [touch locationInView:calloutView]; 

     BOOL isPointInsideView = [calloutView pointInside:touchPoint withEvent:nil]; 
     if (isPointInsideView) 
     { 
      // place your action code. 
     } 

} 

,或者您可以使用,

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCalloutAction:)]; 
    tapGestureRecognizer.delegate = self; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    [calloutView addGestureRecognizer:tapGestureRecognizer]; 

-(void) tapCalloutAction:(id)sender 
{ 
    // do stuff 
} 
+0

組成的答案,但這將爲我服務點擊地圖上的任何地方,但我只想當你點擊我按下的白色框標識我時,我正在使用此代碼來顯示註釋 MKAnnotationView * pin =(MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @「當前」]; – user2652032

+0

如果只按這個視圖,你可以在你的標註視圖上觸摸[觸摸locationInView:yourAnnotationView.calloutView]。 – karthika

+0

MKAnnotationView * pin =(MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@「current」]; CGPoint touchPoint = [touch locationInView:pin]; 我做到了,所以它沒有工作 – user2652032

1

的想法是,你想使所有的「附屬視圖」可點擊沒有與實際標註的原自來水乾擾..這是我要做的事:

第一我創建和分配點擊手勢後,它已被選中,像這樣的註解視圖(我在這裏使用OBJ-C運行時對象協會看到this要點):

// set up vars 
static NSString *const kExtraTapGestureRecognizer = @"extraGesture"; 
UIControl *currentAnnotationControl; 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {  
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
              action:@selector(handleAnnotationViewTap:)]; 
    tap.numberOfTapsRequired = 1; 
    [tap setInfo:kExtraTapGestureRecognizer]; 
    [view addGestureRecognizer:tap]; 

} 

處理水龍頭,我叫MKMapViewDelegate協議的mapView:annotationView:calloutAccessoryControlTapped:,這基本上意味着如果我認爲n變拍了拍附件觀點之間,它是彷彿剛剛拍了拍附件觀點之一:

- (void)handleAnnotationViewTap:(UITapGestureRecognizer *)gestureRecognizer { 
    MKAnnotationView *annotationView = (MKAnnotationView *)gestureRecognizer.view; 
    // currentAnnotationControl is just a reference to one of the 
    // accessory views of the annotation that has just been selected.. 
    // see comment below 
    [self mapView:self.mapView 
    annotationView:annotationView 
    calloutAccessoryControlTapped:currentAnnotationControl]; 
} 

返回annotationView的時候,我保存到像這樣的(左或右)accessoryViews的一個參考:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[Random class]]) 
    { 
     static NSString *annotationIdentifier = @"annotation"; 

     MKAnnotationView *annotationView = 
     (MKAnnotationView *) [self.mapView annotationIdentifier]; 
     if (annotationView == nil) 
     { 
      annotationView = [[MKAnnotationView alloc] 
           initWithAnnotation:annotation reuseIdentifier:RiderAnnotationIdentifier]; 
      annotationView.canShowCallout = YES; 

      UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      leftButton.frame = CGRectMake(0, 0,21, 21); 
      [leftButton setImage:[UIImage imageNamed:@"smallInfo_rider_left.png"] forState:UIControlStateNormal]; 

      [leftButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; 
      annotationView.leftCalloutAccessoryView = leftButton; 

      // this is where i store a reference to one of the accessory views 
      currentAnnotationControl = leftButton; 

      return annotationView; 
     } 
     else 
     { 
      annotationView.annotation = annotation; 
     } 

     return annotationView; 

保持我們創建一個額外的點觸手勢一點,我們必須得到儘快擺脫它作爲附件視圖中的一個已經被挖,像這樣:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    // we remove the extra tap gesture so that it doesn't interfere 
    // with normal app flow in the future 
    for (UIGestureRecognizer *gesture in [view gestureRecognizers]) { 
     if ([[gesture info] isEqualToString:kExtraTapGestureRecognizer]) { 
      [gesture removeTarget:nil action:NULL]; 
     }    
    } 
    // more stuff 
+0

嘿夥計,你的回答並沒有給我解決辦法,當然我誤解了你的例子中的某些東西,這並不好 – user2652032