2014-04-08 99 views
0

我創建iOS和每個標註的地圖點擊我加入類似以下的自定義子視圖:IOS事件單擊

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

    //load pin callout 
    nearbyMapCallout *calloutView = [[nearbyMapCallout alloc]init]; 
    calloutView = (nearbyMapCallout *)[[[NSBundle mainBundle] loadNibNamed:@"nearbyMapCallout" owner:self options:nil] objectAtIndex:0]; 

    CGRect calloutViewFrame = calloutView.frame; 
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 37, -calloutViewFrame.size.height + 35); 
    calloutView.frame = calloutViewFrame; 

    [view addSubview:calloutView]; 

} 

所以現在我怎麼可以觸發事件時,點擊特定的標註標註子視圖?

+0

Hi @ gilm501你有沒有得到任何解決方案呢? –

回答

1

你可以試試這個

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    if(![view.annotation isKindOfClass:[MKUserLocation class]]) { 
     CGSize calloutSize = CGSizeMake(100.0, 80.0); 
     UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y-calloutSize.height, calloutSize.width, calloutSize.height)]; 
     calloutView.backgroundColor = [UIColor whiteColor]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(5.0, 5.0, calloutSize.width - 10.0, calloutSize.height - 10.0); 
     [button setTitle:@"OK" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(checkin) forControlEvents:UIControlEventTouchUpInside]; 
     [calloutView addSubview:button]; 
     [view.superview addSubview:calloutView]; 
    } 
} 

編號:https://stackoverflow.com/a/17772487/2553526

+1

你至少可以引用你的答案的作者... http://stackoverflow.com/questions/17772108/custom-mkannotation-callout-bubble-with-button –

+0

感謝您指出出 –

0

您可以使用UITapGestureRecognizer捕捉到你的觀點的點擊事件,當你添加:

UITapGestureRecognizer *singleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
              action:@selector(handleSingleTap:)]; 
[calloutView addGestureRecognizer:singleTap]; 
[singleTap release]; 

以下是呼籲攻calloutView方法:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer { 
    CGPoint location = [recognizer locationInView:[recognizer.view superview]]; 

    //Add your code here.. 
} 

希望這有助於。

+0

它doesesent工作... – gilm501