如何判断触摸是否在另一个形状的框架上?

And*_*rew 1 iphone view objective-c cgpoint

我知道如何触及位置,所以现在我有一个触摸位置的CGPoint.找出触摸是否超过UIView的最佳方法是什么?我知道的方法:

if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
Run Code Online (Sandbox Code Playgroud)

等等,但这是最好的方式吗?

ugh*_*fhw 5

如果您只是想知道某个点是否在视图的边界内,则可以使用该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");
}
Run Code Online (Sandbox Code Playgroud)