UIMAP 标注子视图单击上的 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];
}

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

你可以

试试这个

- (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

添加

以下内容时,可以使用UITapGestureRecognizer捕获视图的点击事件:

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

以下是点击标注视图时调用的方法:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];
  //Add your code here..
}

希望这有帮助。

以下答案对我有用:

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

它将视图添加到超级视图,但请注意,当您移动地图时,您必须以编程方式移动它。

相关内容

  • 没有找到相关文章

最新更新