当使用hitTest:withEvent方法时 - 决定是否启动了tap

时间:2014-02-23 07:40:09

标签: ios iphone objective-c uitouch uievent

我正在尝试在自定义视图中覆盖hitTest:withEvent。这是一个包含许多子视图的容器视图。

附加到此“superView”是一个手势识别器。在这个视图的顶部有许多子视图(反过来又有越来越多的子视图)我决定过度使用hitTesh:withEvent以决定哪个视图应该获取事件 - 以及在哪种情况下superview应该获取事件以便触发手势。

我遇到的问题是,如果在子视图中初始化轻击手势,hitTest将返回超级视图。在这种情况下,我想“追溯”忽略点击操作 - 并再次“调用”hitTest:withEvent在superview上。然后,超级视图可以决定将“真正的”响应者传递给此事件。

我该怎么做:

示例:

// In the container view

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{

// the default view that would have been returned
  UIView *v = [super hitTest:point withEvent:event];

  // if nil than touch wasn't in this view so disregard
  if (v == nil)
  {
    return nil;
  }

  // if the touch was on a control than I don't want to take care of it
  if ([v isKindOfClass:[UIControl Class]])
  {
    return v;
  }

  // now here is where I need to fix

  // I am only interested in a touch event that would initiated a pan gesture
  // if the event was a tap for intance - than return [super hitTest:point withEvent:event]
  // for instance if a uitableview cell was tapped - I don't want to mask the event
  if (event wouldn't initiate a pan)
  {
    return v;
  }
  return self;
}

0 个答案:

没有答案