riyaz
riyaz

Reputation: 1103

To find what object is underneath our touch when touch moved

How to determine the property or perhaps the object underneath the object i am hovering or dragging?

To put my question clearly, lets take I am hovering a uiview, I want to find out what (object or view) is underneath the view I am hovering.

Upvotes: 0

Views: 74

Answers (2)

Ali Kıran
Ali Kıran

Reputation: 1006

in custom view you can override touchesEnded method.This sample code may help your custom view hit test problem.

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

            if ([touches count] == 1) {
                UITouch *touch = [touches anyObject];
                CGPoint point = [touch locationInView:custom_view];

                if (CGRectContainsPoint(custom_view.bounds, point)) {
                   //if touch hit to custom_view
                };
            }
            [super touchesEnded:touches withEvent:event];
        }

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130193

For one, if you know the frames of both objects you can use CGRectIntersectsRect.

 if (CGRectIntersectsRect(topObjectsRect, bottomObjectsRect)) {
        //
    }

Additionally, you could get the point that was touched and then use the following to check if that point is in a certain rectangle.

  if (CGRectContainsPoint(CGRectMake(someX, someY, someWidth, someHeight), pointOfTouch))
  {
      //
  } 

Upvotes: 1

Related Questions