通过兄弟子视图传播触摸事件?

puz*_*zzl 4 objective-c touch uitouch ios

我有一堆子视图,都有用户交互部分(子)和全屏.问题是,如果我触摸堆栈顶部的非交互式部分,它将不会将该触摸传播到堆栈的其余部分.我的设置:

查看A - 查看B(全屏容器,本身不是交互式但具有交互式子视图)----查看B1(交互式)----查看B2(交互式) - 查看C(与B相同)----查看C1(交互式)----查看C2(交互式)

B和C都是全屏,但B1/B2/C1/C2只是屏幕的一小部分.

[a addSubview:b];
[a addSubview:c];
Run Code Online (Sandbox Code Playgroud)

如果我触摸C1/C2之外的任何东西,我想触摸事件然后检查它是否击中B(B1/B2)内的任何地方,但它只是回到A,然后回到A的父母.是否有可能做到这一点?如果我在C上设置了userInteractionEnabled NO,但在C1/C2上设置为YES,它也没有得到对内部的任何调用,尽管在这种情况下,B会按预期获得触摸.

编辑:结束遍历视图堆栈以仅检查某些子视图而不是所有子视图:

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

    if (self != self.topCustomViewsContainer) {     
        for (UIView *v in self.createdSubviews) {
            CGPoint newPoint = point;
            newPoint.x -= v.frame.origin.x;
            newPoint.y -= v.frame.origin.y;
            UIView *hit = [v hitTest:newPoint withEvent:event];
            if (hit)
                return hit;
        }
        return nil;

    }

    return [super hitTest:point withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

bas*_*bas 8

一种可能性是覆盖

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

视图B中的方法.只有在击中B的子视图时才能使其返回.试试这样:

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