有一种方法可以让用户在视图外触摸时,应用程序检测到该视图内的近点?我想像下面的图片一样进行检测。

编辑:

CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

if (CGRectContainsPoint([_grayView frame], touchPoint)) {
    // The touch was inside the gray view
} else {
    // The touch was outside the view. Detects where's the closer CGPoint inside the gray view.
    // The detection must be related to the whole view (In the image example, the CGPoint returned would be related to the whole screen)
}

最佳答案

static float bound(float pt, float min, float max)
{
    if(pt < min) return min;
    if(pt > max) return max;
    return pt;
}

static CGPoint boundPoint(CGPoint touch, CGRect bounds)
{
    touch.x = bound(touch.x, bounds.origin.x, bounds.origin.x + bounds.size.width;
    touch.y = bound(touch.y, bounds.origin.y, bounds.origin.y + bounds.size.height;
    return touch;
}

10-07 19:54
查看更多