iOS – 如何将触摸转发到底层的uiview

我有3个相同大小的UIViews堆叠在一起。 最顶端是透明的,只用于检测触摸。 检测到的触摸types将决定我想要接收触摸事件的其他两个基础视图中的哪一个。 一旦最上面的视图完成触摸,我需要将触摸事件转发到正确的基础视图。 我怎样才能做到这一点?

编辑 – 我正在添加我的触摸检测代码。 这是在MainViewController中,其视图包含所有3个子视图。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { if (touch.view == self.touchOverlay) { CGPoint touchLocation = [touch locationInView:touch.view]; //do a bunch of math to determine which view should get the touches. if (viewAshouldGetTouches) //forward to viewA if (viewBshouldGetTouches) //forward to viewB } } } 

使你的两个子视图setUserInteractionEnabled:NO并处理父项中的所有触摸。 然后,根据触摸types,将正确的视图发送给程序触摸事件。 那么你不需要你清楚的看法。 基本上你会自下而上协调触摸事件,而不是去顶部 – >底部 – >中间。

你必须通过为你的顶视图创build一个UIView子类并覆盖下面的方法来做到这一点:

 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { // UIView will be "transparent" for touch events if we return NO return (point.y < MIDDLE_Y1 || point.y > MIDDLE_Y2); }