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

标签 iphone ios touch mkmapview mkannotationview

我有一个自定义的 MKAnnotationView。在它的 setselected:animated 方法中,我向它添加了一个从 Nib 加载的自定义气泡,调整注释 View 的框架以包含此 View 并使用其他颜色重新绘制注释圆,像这样(首先 - 不是选中,第二个 - 选中,蓝色 - 框架,绿色 - alpha = 0.8 的自定义气泡 View ,红色 - 注释 View ):

enter image description here

它工作正常,气泡出现,并且只能通过点击它的外部来“关闭”(这就是我增加框架的原因)。我在这个气泡上有一些按钮,如果注释下方只有 map ,则可以单击它们。

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

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

谢谢

编辑 : 更短:如果下面有其他 MKAnnotaionView,为什么我可以点击在 MKAnnotationView 的 addSubview 中添加的 UIView这个 UIView ?

详细信息:

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

最佳答案

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

The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain.

MKAnnotationView 是 UIVIew 的子类。因此,当您的注释获得触摸时,它会将其传递给它的父类(super class)并在响应者链上向上传递,因此最终您的 map View 获得触摸并激活被覆盖的注释。

要解决这个问题,请在您的 annotationView 类中实现触摸处理方法,并且不要将触摸事件向上传递到响应链。

关于iphone - 防止触摸传播到下面的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10295147/

相关文章:

iphone - Cocoa Touch/iPhone 中的 XML 解析

iphone - 如何检测 UIView 大小何时改变?

ios - 一起使用 CGContextFillPath 和 CGContextStrokePath? iOS

ios - 从 TextBox 到 Widget 的文本?

objective-c - 检测用户在特定时间内没有触摸屏幕

iphone - Objective C - 访问普通 C 数组

iphone - 为什么iphone View 大小设置为480

ios - glReadPixels 在 iOS 上崩溃

wpf - 禁用鼠标升级

angularjs - ng-touch Angular 库真的只支持水平滑动而不支持垂直滑动吗?