通过兄弟子视图传播触摸事件? [英] Propagating touch events through sibling subviews?
问题描述
我有一堆子视图,都有用户交互部分(子)和全屏。问题是,如果我触摸堆栈顶部的非交互式部分,它将不会将该触摸传播到堆栈的其余部分。我的设置:
I've got a stack of subviews that are all have user interactive sections (children) and all full screen. The problem is that, if I touch down on a non-interactive section at the top of the stack, it won't then propagate that touch across the rest of the stack. My setup:
查看A
- 查看B(全屏容器,本身不是交互式但具有交互式子视图)
---- view B1(互动)
----查看B2(互动)
- 查看C(与B相同)
----查看C1(互动)
--- -view C2(交互式)
view A --view B (full screen container, not itself interactive but has interactive subviews) ----view B1 (interactive) ----view B2 (interactive) --view C (same as B) ----view C1 (interactive) ----view C2 (interactive)
B和C都是全屏,但B1 / B2 / C1 / C2只是屏幕的一小部分。
B and C are both full screen, but B1/B2/C1/C2 are only small sections of the screen.
[a addSubview:b];
[a addSubview:c];
如果我触摸C1 / C2以外的任何东西,我想触摸事件然后检查是否它击中了B(B1 / B2)内的任何地方,但它只是回到A,然后回到A的父母。是否有可能做到这一点?如果我在C上设置了userInteractionEnabled NO,但在C1 / C2上设置为YES,它也没有得到任何内部调用,尽管在这种情况下B会按预期得到触摸。
If I touch anything outside of C1/C2, I'd like the touch event to then check if it hit anywhere inside of B (B1/B2), but instead it just goes back to A, and then to A's parent. Is it possible to do this? If I set userInteractionEnabled NO on C but YES on C1/C2, it doesn't get any calls to the inner ones either, although in this case then B would get the touches, as expected.
编辑:结束遍历视图堆栈以仅检查某些子视图而不是所有子视图:
edit: Ended up traversing the view stack manually to check only for certain subviews and not all of them:
- (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];
}
推荐答案
一种可能性是覆盖
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
视图B中的方法。只有在击中B的子视图时才能返回。试试这样:
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;
}
}
这篇关于通过兄弟子视图传播触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!