Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7692371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:50:16+00:00 2026-05-31T20:50:16+00:00

I have two views each of which contain two subviews. Hit detection is working

  • 0

I have two views each of which contain two subviews.

Hit detection is working fine as long as the two top views do not overlap.
Hence I can touch subview marked A in the left side of the image below.

However as soon as the top two views overlap, there is just no way that the A view receives touches as view 1 is “above” view 2 and “eats” the touch.

Both View 1 and View 2 detect touches, as they can be moved around, hence need to detect and react to touches “in between” subviews.

This means that my two “top views” detection should say: “Oh, wait a minute, maybe I am ovelapping some other view and should pass the event to it, and only start a drag/move around if and only if no other view is ‘below me'”.

How would I do that?
enter image description here

Edit:
Thanks jaydee3

This didn’t work at first, resulting into an infinite recursion: each view deferring to its sibling, which in turn defers back to the initiating view:

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView * hit =  [super hitTest:point withEvent:event] ;

    if (hit == self) {
        for (UIView * sibling in self.superview.subviews) {
            if (sibling != self) {
                CGPoint translated = [self convertPoint:point toView:sibling] ;
                UIView * other = [sibling hitTest:translated withEvent:event] ;
                if (other) {
                    return other ;
                }
            }
        }
    }

    return hit ;
}

So, I added a “marked set” to keep track of which view already had been visited, and it now all works 🙂

- (UIView *) hitTest: (CGPoint) point withEvent: (UIEvent *) event {

    static NSMutableSet * markedViews = [NSMutableSet setWithCapacity:4] ;

    UIView * hit =  [super hitTest:point withEvent:event] ;

    if (hit == nil) return nil ;

    if (hit == self) {
        for (UIView * sibling in hit.superview.subviews) {
            if (sibling != hit) {
                if ([markedViews containsObject:sibling]) {
                    continue ;
                }

                [markedViews addObject:sibling] ;
                CGPoint translated = [hit convertPoint:point toView:sibling] ;
                UIView * other = [sibling hitTest:translated withEvent:event] ;
                [markedViews removeObject:sibling] ;

                if (other) {
                    return other ;
                }
            }
        }
    }

    return hit ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:50:17+00:00Added an answer on May 31, 2026 at 8:50 pm

    Make a custom subclass for your view (that containts the other two subviews) and overwrite the hitTest: method for it. In that method, check if the hitTest view is one of the two subviews, else return nil. So all touches will be ignored on your surrounding view. Resulting in a touch on the view below, which can handle it itself.

    //edit: (You get the hitTest view by calling UIView* view = [super hitTest:withEvent:];.)

    //edit2: i thought more of smth like that: 🙂

    - (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        UIView * hit =  [super hitTest:point withEvent:event] ;
    
        if (hit == self) {
            return nil;
        }
    
        return hit ;
    }
    

    😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xib with a view which contains two views which each contain
I've got a table recording views of programs. Each program can have two different
In my case, I have a ListView which contains two kinds of views, folders
I have a custom list view which contains two text views and on button.
I have picker controls on a single view with two outlets for each. When
I have two views, one view takes the whole screen, the second view covers
I have two views within one .xib (one view for landscape, another for portrait).
I have two views that I would like to combine into one. The first
I have two views.In first view, I have a table view and I am
I have a repositories, i want to have two views: Developers can see the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.