Propagating touch events through sibling subviews?

三世轮回 提交于 2019-12-04 09:53:20

One possibility is to overwrite the

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

method in view B. You can make it return only if a subview of B is hit. Try it like this:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  UIView *hitView = [super hitTest:point withEvent:event];
  if (hitView == self) {
    return nil;
  } else {
    return hitView;
  }
}

Events propagate up the view hierarchy from child to parent (as you discovered). So two possibilities occur to me:

Make C a child of B. They both have the same size, and C obviously already has a transparent background, so add it to B and use [viewB bringSubviewToFront:viewC] to make it the first to receive touches. Anything it doesn't capture will pass to views underneath it, then up to parent A.

Or: Manually capture touches, and force them over to view B (sibling of C). This involves implementing this in your view controller class

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [viewC touchesBegan:touches withEvent:event];
    // Note this could be in sibling B or in parent A, depending on which
    // has a reference to the 'viewC' object
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!