0

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

}

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

4

3 回答 3

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

于 2014-04-09T09:50:17.550 回答
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..
}

希望这可以帮助。

于 2014-04-08T20:17:34.573 回答
0

以下答案对我有用:

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

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

于 2014-08-13T23:36:11.707 回答