Question

I know how to get the position touched, so now i have a CGPoint with the touched position. What's the best way to find out if the touch is over a UIView or not? I know of the method of:

if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x

etc., but is this the best way of going about it?

Was it helpful?

Solution

If you simply want to know if a point is inside the bounds of a view, you can use the pointInside:withEvent: method.

CGPoint touchPoint = [theTouch locationInView:theView];
// If the point was retrieved for a different view, it must be converted to the coordinate space of the destination view using convertPoint:fromView:
if([theView pointInside:touchPoint withEvent:nil]) {
    NSLog(@"point inside");
}

OTHER TIPS

Ugh's answer covers views, which I believe is what you want; if you have an arbitrary CGRect, you can use CGRectContainsPoint:

BOOL isInside = CGRectContainsPoint(myRect, touchPoint);

Yes, CGRectContainsPoint will save you from writing so many comparision equations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top