Reputation: 16051
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?
Upvotes: 1
Views: 734
Reputation: 31
Yes, CGRectContainsPoint will save you from writing so many comparision equations.
Upvotes: 0
Reputation: 64002
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);
Upvotes: 3
Reputation: 39905
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");
}
Upvotes: 5