Detect if certain UIView was touched amongst other UIViews

Related searches

I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of buttons in the second UIView and a UITable in the 3rd UIView.

Problem is I turn userInteractionEngabled on on the first view and that works, but all the other views respond in the same way even if I turn it off. If I disable userInteractionEnabled on self.view none of them respond. I also can't detect which view was touched in the touchesBegan delegate method.

My code:

UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aView = userInteractionEnabled = YES;
[self.view addSubview:aView];

UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 50)];
bView.userInteractionEnabled = NO;
[self.view addSubview:bView];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//This gets called for a touch anywhere

In order to check whether certain view inside another view was touched you can use hitTest.

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

In your custom implementation of touchesBegan check every touch in touches set. The point for hitTest method can be obtained using

- (CGPoint)locationInView:(UIView *)view;

method, where the view is your superView (the one that contains other views).

EDIT: Here's a fast custom implementation:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint locationPoint = [[touches anyObject] locationInView:self];
    UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];

I hope this was helpful, Paul

Handling Touches in Your View, I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of� public CustomUIGestureRecognizer (UIView view) this._view = view; Then, inside the class “CustomUIGestureRecognizer”, override the event handler “TouchesMoved”, and detect if the touch location is outisde the view

In my case the UIView I wanted to find was not returned by the hitTest. But the following code worked better:

    CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
    CGPoint viewPoint = [myImage convertPoint:locationPoint fromView:self.view];
    if ([myImage pointInside:viewPoint withEvent:event]) {
       // do something

Detect if certain UIView was touched amongst other-第2页回答- 码农岛, If we want to detect taps on more UIView s, we will find ourselves repeating: so another object needs to hold a strong reference to it; otherwise, it will be weak the reference between the Tap and his view, so later, when we found a A drawback of this approach, besides touching the guts of the UIView� Dragging my finger around inside the control resulted in many events firing during the drag, but I still could not touch outside the control, drag my finger inside, and get an event to fire. After doing a bit of research, I found that UIKit is designed so that a view will not receive touch events for any touch that did not begin in that view.

swift code:

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            if touch.view == self.view {
            } else {


Tap detection on iOS views without repeating ourselves., That will make position a CGPoint representing where the user touched in the current view. You can if you want pass a different view to location(in� Passing through touches to UIViews underneath (4) I have a UIView with 4 buttons on it and another UIView on top of the buttons view. The top most view contains a UIImageView with a UITapGestureRecognizer on it.

This can also be done the following way.

First find the location of the touch in the view you care about:

CGPoint location = [touches.anyObject locationInView:viewYouCareAbout];

Then see if the point is within the bounds of the view:

BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location);

Then if it's within bounds, perform your action.

This can be done with one statement, which for example, can be used directly within an if statement:

CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout])

How to find a touch's location in a view with location(in:), This chapter discusses touch detection and response by views (and other If every UITouch in a UIEvent associated with a certain UIView has the phase Imagine, for example, a view that distinguishes between a finger tapping briefly and a� UIViewController is a Cocoa Touch class built for the purpose of managing UIViews. It expects to have a view hierarchy, but you don't "automatically" get a view (this is slightly inaccurate; see edit below).

In touchesBegan check that the touched view is the one you want, solved the problem for me when I tried to identified if the user touched a specific ImageView.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if ([touch view] ==  imageView ) {
        ... your code to handle the touch

If you adjust the content for the view diamensions make sure to change the view size accordingly otherwise it will recognize touch event even in area where there in so content in the view. In my case when I tried to keep the image proportion with UIViewContentModeScaleAspectFit although the image was adjusted as required in the "white spaces" areas touch event were catched as well.

Chapter 18. Touches, Q&A for users of Linux, FreeBSD and other Un*x-like operating systems Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

8 Detect if certain UIView was touched amongst other UIViews 7 iOS RTL - improperly displaying English inside RTL string View more network posts →

Touch events travel recursively down through the stack of UIViews, to check if the touch event is within the bounds of a view object. This is often called Hit-testing. They will first be called on the topmost UIView or UIViewController and then will be called on the UIView and UIViewControllers below them in the view hierarchy.

In addition to handling touch events directly, views can also use gesture recognizers to detect taps, swipes, pinches, and other types of common touch-related gestures. Gesture recognizers do the hard work of tracking touch events and making sure that they follow the right criteria to qualify them as the target gesture.

  • Sorry, I know you are on the right track but I'm having a little trouble understanding exactly what you are saying. I am getting the point touched from touches from your locationInView and I could technically work out which uiview the user touched from this but I'm not sure how it calls hitTest
  • It should be noted that calling locationInView: with a parameter of nil gives you the location CGPoint in the coordinate space of the main window.
  • I also ran into issues with the accepted answer. For whatever reason, this worked as well. Note that I was also trying to detect hits on a UIImageView and not just a UIView if that is relevant at all.
  • great answer for swift. Sharing the love with the up vote.