0

何度もグーグルで調べても解決策が見つからないので、質問します。

私の質問は次のとおりです。-ビューを特定の領域にドラッグしたいのですが、それを超えて、ビューをドラッグしたくありませんが、指定されたビューでドラッグできる必要があります。それを超えてドラッグするとドラッグを停止できますが、許可された領域でドラッグするためにタッチしてもドラッグしません。

My code:-
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

        UITouch *touch=[touches anyObject];
        CGPoint pt = [[touches anyObject] locationInView:touch.view];
        startLocation = pt;
    }

    - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
        // Move relative to the original touch point
        UITouch *touch=[touches anyObject];
        CGPoint pt = [[touches anyObject] locationInView:touch.view];
        CGPoint pt2 = [[touches anyObject] locationInView:self.view];

        if ([touch.view isKindOfClass:[MyView class]]) {

            CGRect frame = [touch.view frame];
            CGRect prevFrame=[touch.view frame];
            frame.origin.x += pt.x - startLocation.x;
            frame.origin.y += pt.y - startLocation.y;
            CGRect boundsA = [touch.view convertRect:touch.view.bounds toView:nil];
            CGRect boundsB = [self.canvasVw convertRect:self.canvasVw.bounds toView:nil];

                Boolean viewsOverlap = CGRectContainsRect(boundsB,boundsA );
                if (viewsOverlap==YES) {
                    NSLog(@"intersects");
                    [touch.view setFrame:frame];

                }
                else{
                    NSLog(@"not intersects");


    }
        }
      }

コードの問題は、ビューをドラッグすると、領域の外に出て再びドラッグしないことです。

画像を参照してください:- ここに画像の説明を入力

黒い領域ではなく白い領域のみに赤いビューをドラッグする必要があります。これを行う方法を教えてください。ありがとう。

4

1 に答える 1

1

ええと、基本的には境界です。退屈そうに見えますが、次のことを確認してください。

  • 赤の self.frame.position.x は < 0 ではありません (ゼロはその親の位置です)
  • self.frame.position.x + self.frame.size.width は、self.superview.size.width よりも > 大きくありません

Y についても同じロジックです。


編集1:

double possibleNewX = pt.x - startLocation.x;

if (((self.frame.position.x + possibleNewX) > 0) && ((self.frame.size.width + self.frame.position.x + possibleNewX) < self.superview.frame.size.width))
{
   frame.origin.x += possibleNewX;
}

double possibleNewY = pt.y - startLocation.y;

if (((self.frame.position.y + possibleNewY) > 0) && ((self.frame.size.height + self.frame.position.y + possibleNewY) < self.superview.frame.size.width))
{
   frame.origin.y += possibleNewY;
}
于 2012-11-13T18:02:54.510 に答える