iOS - how to forward touches to the underlying uiview

≡放荡痞女 提交于 2020-01-05 07:40:50

问题


I have 3 UIViews of the same size stacked on top of each other. The topmost is transparent and only used for detecting touches. The type of touch detected will determine which of the other two underlying views I want to receive the touch events. Once the topmost view is finished with the touch, I need to forward the touch events to the correct underlying view. How can I do that?

EDIT - I am adding my touch detection code. This is within MainViewController, whose view contains all 3 subviews.

-(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


        }
    }
}

回答1:


Make your two subviews setUserInteractionEnabled:NO and handle all touches in the parent. Then, depending on touch type, send the correct view a programatic touch event. Then you don't need your clear view on the top. Basically you will be coordinating touch events from the bottom up instead of going top->bottom->middle.




回答2:


You'll have to do this by creating a UIView subclass for your top view and overriding the following method :

- (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);
}


来源:https://stackoverflow.com/questions/14654557/ios-how-to-forward-touches-to-the-underlying-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!