如何创建响应触摸的标注气泡 MKAnnotationView

发布于 2024-12-22 13:57:06 字数 958 浏览 3 评论 0原文


我正在尝试使自定义注释视图能够响应触摸,但没有成功。
感谢这个问题,我能够制作一个接近我想要的注释视图自定义注释视图的标注气泡?也看到了这个如何捕获触摸而不关闭标注? 但问题完全不同 到目前为止,我做的第一件事是子类化 MKAnnotationView 并覆盖 -setSelected:animated: 方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  if(selected)
  {
    MyCallOut * callOut=[MyCallOut createMyCallOut];
    callOut.tag=555;
    [self.superview addSubview:callOut];

  }
  else
  {
    // [[self viewWithTag:555] removeFromSuperview];
    //Remove my custom CallOut
  }
}

问题是地图视图正在吃掉所有触摸事件,我的自定义标注有两个按钮,但没有触发任何操作压迫他们。

在我的一项实验中,我尝试将标注视图添加到 MKAnnotationView 超级视图(mapView)中,当我滚动时,一切似乎都很好,但如果我缩放,标注会四处移动。

I'm trying to make a custom annotation view able to respond to touches without success.
Thanks to this question I was able to made an annotation view close to what I want customize callout bubble for annotationview? also seen this How to capture touches and NOT dismiss the callout? but the problem is quite different
The first thing I've done so far is subclassing an MKAnnotationView and override the -setSelected:animated: method

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  if(selected)
  {
    MyCallOut * callOut=[MyCallOut createMyCallOut];
    callOut.tag=555;
    [self.superview addSubview:callOut];

  }
  else
  {
    // [[self viewWithTag:555] removeFromSuperview];
    //Remove my custom CallOut
  }
}

The problem is that the map view is eating all the touches event, my custom callout has two buttons but no action is triggered pressing them.

In one of my experiment I've tried to add the callout view to the MKAnnotationView superview (the mapView) everything seems to be fine while I scroll, but If I zoom the callout moves around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2024-12-29 13:57:06

您必须在地图注释中添加呼出附件视图

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed; 
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return pinView;



} 
else {
    [mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}

以及呼出的点击事件

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control   
{

}

you have to add call out accessory view in map annotation

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed; 
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return pinView;



} 
else {
    [mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}

and for click event of your call out

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control   
{

}
゛时过境迁 2024-12-29 13:57:06

解决了查看此链接自定义注释和标注,另一个棘手的部分是实现正确的方法按假标注上的按钮。我管理这部分创建一个布尔值,该值了解屏幕上注释/parentAnnotation 的数量,并选择何时可以消失或不消失。只需使 CalloutAnnotation 可供选择。

Solved looking to this link Custom annotation and callouts, the other tricky part is implement a correct way to press button on the fake callout. I managed this part creating a boolean value that understands the number of annotation/parentAnnotation on screen and choose when one could disappear or not.Just need to make the CalloutAnnotation available to selection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文