防止触摸传播到下面的注释

时间:2012-04-24 09:25:04

标签: iphone ios touch mkmapview mkannotationview

我有自定义MKAnnotationView。在我的setselected:animated方法中,我添加了一个从笔尖加载的自定义气泡,调整注释视图的框架以包含此视图,并用其他颜色重绘注释圆,如下所示(首先 - 未选中,第二 - 选中) ,蓝色 - 框架,绿色 - 自定义气泡视图,alpha = 0.8,红色 - 注释视图):

enter image description here

它工作正常,气泡出现,并且只能通过敲击它来“关闭”(这就是为什么我增加了框架)。我在这个气泡上有一些按钮,如果注释只有地图下面没有任何内容,它们都是可点击的。

但是当在标注气泡下有另一个注释我可以点击“穿过”整个气泡。当我点击其中一个按钮时,会出现点击突出显示,但会选择其他注释,因为didSelectAnnotationView会触发......

我试图让泡泡不透明/半透明,没有运气;在按钮上设置exclusiveTouch,在视图本身上,没有运气;尽量不要弄乱框架,仍然可以点击。 我错过了什么吗?

由于

修改更短:如果在此UIView下还有其他MKAnnotaionView,为什么我可以点击MKAnnotationView中UIView中添加的addSubview

详细信息:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  if(selected)
  {
    initialFrame = self.frame;       // save frame and offset to restore when deselected
    initialOffset = self.centerOffset;  // frame is correct for a circle, like {{2.35, 1.47}, {12, 12}}

    if (!self.customCallout) 
    {
      self.customCallout = [[[NSBundle mainBundle] loadNibNamed:@"CustomCallout" owner:self options:nil] objectAtIndex:0];
    }
    // adjust annotationview's frame and center
    // callout is 200x120, here frame is {{2.35, 1.47}, {200, 132}} 
    self.customCallout.layer.cornerRadius=5;
    self.customCallout.exclusiveTouch = YES;
    [self addSubview:self.customCallout];
  }
...
}

initWithAnnotation有以下内容:

   self.canShowCallout = NO;  // to appear the subview
   self.exclusiveTouch = YES; // ...
   self.enabled = YES;
   self.opaque = YES;

4 个答案:

答案 0 :(得分:3)

触摸处理方法的默认行为(touchesBegan:touchesEnded:等)在documentation中有此注释:

  

此方法的默认实现不执行任何操作。但是,UIResponder的UIKit子类,特别是UIView,会立即将消息转发给响应者链。

MKAnnotationView是UIVIew的子类。因此,当您的注释获得触摸时,它会将其传递给它的超类并响应响应者链,因此最终您的地图视图会触摸并激活覆盖的注释。

要解决,请在annotationView类中实现触摸处理方法,不要将响应事件传递给响应者链。

答案 1 :(得分:0)

你可以先检查哪个事件发生火灾

1, the button on bubble
2, didSelectAnnotationView

如果泡沫上的按钮先点火 你可以通过继承

来交换泡泡中的触摸
touchesBegan:touches
touchesMove:touches
touchesEnd:touches

在气泡视图中阻止传播

答案 2 :(得分:0)

我认为您会发现以下链接非常有用:

http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-2/

How do I make a MKAnnotationView touch sensitive?

第一个链接讨论(除其他事项外)如何防止传播,第二个链接讨论如何检测触摸。

答案 3 :(得分:0)

我最近遇到过这个问题,花了很多时间,我没有。解决它,实际上最终变得非常简单,当然只有在花了很多年后才意识到这一点。

您只想将MapView子类化,并在该子类中继承以下内容:

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    debugPrint(gestureRecognizer)
    if gestureRecognizer is UITapGestureRecognizer {
        return true
    } else {
    return false
    }
}

原因是,地图交互的所有处理,例如缩放平移等,似乎都在内部处理,我们无法访问。然而,导致我们出现问题的手势是可以访问的,但对于我的情况,我只想要处理选择和取消选择注释的轻击手势。