我知道如何获得感动的位置,所以现在我有一个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");
}
Ugh的回答涵盖的意见,我相信是你想要的;如果你有一个任意CGRect
,您可以使用CGRectContainsPoint
:
BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
是,CGRectContainsPoint将节省您写了这么多比较方程。