【发布时间】:2011-05-19 22:30:40
【问题描述】:
我知道如何触摸位置,所以现在我有一个触摸位置的 CGPoint。找出触摸是否在 UIView 上的最佳方法是什么?我知道的方法:
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
等等,但这是最好的方法吗?
【问题讨论】:
标签: iphone objective-c view cgpoint
我知道如何触摸位置,所以现在我有一个触摸位置的 CGPoint。找出触摸是否在 UIView 上的最佳方法是什么?我知道的方法:
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
等等,但这是最好的方法吗?
【问题讨论】:
标签: iphone objective-c view cgpoint
如果您只是想知道一个点是否在视图的边界内,您可以使用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");
}
【讨论】:
Ugh 的回答涵盖了视图,我相信这是您想要的;如果你有一个任意的CGRect,你可以使用CGRectContainsPoint:
BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
【讨论】:
是的,CGRectContainsPoint 会让你免于编写如此多的比较方程。
【讨论】: