我知道如何触摸位置,所以现在我有一个触摸位置的 CGPoint。找出触摸是否在 UIView 上的最佳方法是什么?我知道的方法:
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
等等,但这是最好的方法吗?
我知道如何触摸位置,所以现在我有一个触摸位置的 CGPoint。找出触摸是否在 UIView 上的最佳方法是什么?我知道的方法:
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
等等,但这是最好的方法吗?
如果您只是想知道一个点是否在视图的边界内,您可以使用该pointInside:withEvent:
方法。
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");
}
呃的回答涵盖了观点,我相信这是你想要的;如果你有一个任意的CGRect
,你可以使用CGRectContainsPoint
:
BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
是的,CGRectContainsPoint 将使您免于编写如此多的比较方程。