Search code examples
iosxcodeuiviewuitouch

Get the uiview that is at the location of a touch


Is there a way to quickly get what views are are at (contain) the specific point of a UITouch? I have the coordinates relative to self.view but would like to know any/all of the views that are at that point. My view hierarchy is similar to

  • self.view
    • scrollview
      • view
      • view

Solution

  • You can use the CGRectContainsPoint() method. This method will return a boolean value. You need to pass the views frame(CGRect) and the coordinates relative to touch(CGPoint).

    https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html#//apple_ref/c/func/CGRectContainsPoint

    Also you can check with this method too,

    CGPoint aPoint = //your touch point;
    BOOL isPointInsideView = [yourView pointInside:aPoint withEvent:nil];
    

    Here the given point (aPoint) is checked with the view you are giving (yourView) and returns a boolean value.