iOS版 - 如何转发接触到底层的UIView(iOS - how to forward touch

2019-10-17 15:46发布

我已经堆叠在彼此顶部上的相同大小的3个UIViews。 最上面的是透明的,仅用于检测触摸。 检测到触摸的类型将决定我想收到的触摸事件,其中另外两个基本观点。 一旦最顶部视图与触摸完成,我需要触摸事件转发到正确的基础视图。 我怎样才能做到这一点?

编辑 - 我加入我的触摸检测代码。 这是内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


        }
    }
}

Answer 1:

让你的两个子视图setUserInteractionEnabled:NO和处理所有触摸父。 然后,根据触摸类型,发送正确的观点程序上的触摸事件。 然后,你不需要在上面的清晰视界。 基本上,你会从底部协调触摸事件了,而不是去顶>自下而上>中部。



Answer 2:

你必须通过你的顶视图中创建一个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);
}


文章来源: iOS - how to forward touches to the underlying uiview