问题描述
|
我知道如何触摸位置,所以现在我有一个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\");
}
,Ugh的答案涵盖了各种观点,我认为这是您想要的;如果您有任意的CGRect
,则可以使用CGRectContainsPoint
:
BOOL isInside = CGRectContainsPoint(myRect,touchPoint);
,是的,CGRectContainsPoint将使您不必编写那么多比较方程。