Detect a touch on a species after another species?

I have two views

  • the top view has some opaque and some transparent areas.
  • In the lower view there are several buttons with buttons.

The top view completely covers the bottom view, but since the top view has transparent areas, the bottom view can still be seen.

BUT, I can’t detect button presses in the bottom view anymore, since topview blocks it, what should I do?

Is there a way for the top view to go through the touch of the bottom view?

+6
source share
3 answers

My solution for my own question, hope this helps someone.

In the front-panel view, listen for the touchesEnded:withEvent .

When this delegate fires, you know that the user is touching the front view.

Then you need to check if the finger position touches the special areas in the BOTTOM view.

What to do:

1) Convert the point relative to the view from below:

 UITouch *touch = [touches anyObject]; CGPoint touchPointInLowerView = [touch locationInView:self.lowerViewController.view]; BOOL isLowerButtonClicked = [self.lowerViewController isFingerOnYourButton:touchPointInLowerView]; if(isLowerButtonClicked) { // lower button clicked } 

2) In the lower view

 - (BOOL) isFingerOnYourButton:(CGPoint)point { return CGRectContainsPoint(self.aButton.frame, point); } 

voila. Thus, we can detect clicks in the lower view, even if it is blocked by another interactive view from above.

+4
source

Disable user interaction in the upper view, which blocks from the bottom:

 topView.userInteractionEnabled = NO; 
+1
source

If you do not want the top view (or any of its subzones) to generally respond to touches, you can set the userInteractionEnabled property to NO for this view and make it with it.

Otherwise, it is best to override pointInside:withEvent: or hitTest:withEvent: in the top view class. If the top view and the bottom view are siblings, this is enough to return NO from pointInside:withEvent: if they are further separated in the view hierarchy, you may need to override hitTest:withEvent: to explicitly return the view below for transparent areas.

+1
source

Source: https://habr.com/ru/post/894102/


All Articles