0

After googling a lot and still not finding any solution, I am asking a question.

My question is:- I want to drag my view in a particular area, beyond that, I do not want my view to drag but it should be draggable in the specified view. I am able to achieve to stop dragging if is drags beyond but again when I touches it to drag in the permitted area it does not drag.

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");


    }
        }
      }

The problem in the code is suppose I drag the view , and it goes outside the area then it does not drag again.

See the image:- enter image description here

I need to drag the red view in only white area not the black area. Please suggest me how to do this. Thanks.

Gypsa
  • 11,230
  • 6
  • 44
  • 82

1 Answers1

1

Well, boundaries, basically. As much as boring as it seems, make sure:

  • the self.frame.position.x of the red is not < 0 (zero is the position of its parent)
  • the self.frame.position.x + self.frame.size.width is not > than self.superview.size.width

The same logic for the Y.


Edit 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;
}
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • can you please elaborate in coding terms. – Gypsa Nov 13 '12 at 18:12
  • Check my edit. I did it from the top of my head, sorry if has a typo. – Rui Peres Nov 13 '12 at 18:19
  • The answer really worked like a charm for me, but I have to implement the rotation and dragging feature Now if I rotated my view and then dragging it, it is showing wired behaviour. May you please help me on this. – Gypsa Nov 16 '12 at 12:45
  • The rotation, is a matter of taking care of the Auto-size mask of the `UIView`, play a bit with it, I don't know it by heart. – Rui Peres Nov 16 '12 at 13:32