2

ユーザーがビューの外側に触れたときに、アプリがこのビュー内のより近いポイントを検出する方法はありますか? 下の画像のように検出したい。

例

編集:

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)
}
4

3 に答える 3

0

このコードを試してください!

CGPoint pt = [touches locationInView:childView];
if(pt.x >= 0 && pt.x <= childView.frame.size.width 
&& pt.y >= 0 && pt.y <= childView.frame.size.height) {
   NSLog(@"Touch inside rect");
   return;
}
pt.x = MIN(childView.frame.size.width, MAX(0, pt.x));
pt.y = MIN(childView.frame.size.height, MAX(0, pt.y));
// and here is the point
NSLog(@"The closest point is %f, %f", pt.x, pt.y);
于 2012-11-03T16:19:12.107 に答える
0

必要なのはちょっとした数学だけです:

  1. locationInView:問題のビューを引数としてタッチしてください。
  2. xポイントのをビューの と比較し、boundsその極値にクランプしCGRectます。
  3. ステップ 3 はありません。上記の結果は、探しているポイントです。
于 2012-11-03T16:02:08.133 に答える
0
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;
}
于 2012-11-03T16:15:09.263 に答える