关于uimap callout子视图的IOS事件点击

时间:2014-04-08 20:06:46

标签: ios annotations callout

我在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];

}

那么现在如何在单击特定注释标注子视图时触发事件?

3 个答案:

答案 0 :(得分: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 :(得分: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..
}

希望这有帮助。

答案 2 :(得分:0)

以下答案对我有用:

https://stackoverflow.com/a/17772487/2915167

它为超级视图添加了一个视图但是要小心,当你移动地图时,你必须以编程方式移动它。