简体   繁体   中英

How do i find out if a touch is over the frame of another shape?

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?

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");
}

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);

是的,CGRectContainsPoint将使您不必编写那么多比较方程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM