UIPanGestureRecognizer limit to coordinates

Phillip picture Phillip · Jul 11, 2012 · Viewed 8.4k times · Source

I added to my main UIView a subview (called panel) and i added gestureRecognizer to it because i want it to be draggable only for the Y axis and only for certain limits (i.e. 160, 300, over 300 it can't go).

I implemented the gesture handling that way

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(self.view.frame.size.width/2, recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view.superview];

    //now limit the drag to some coordinates
   if (y == 300 || y == 190){
       no more drag
    }
}

but now i don't know how to limit the drag to those coordinates.

It's not a huge view, it's just a small view containing a toolbar and a button.

How can i limit the drag to a coordinate? (x = 160(middle screen), y =404 ) <- example

What should the center be there?

I googled a lot but i didn't find a concrete answer.

Thanks in advance

Answer

rob mayoff picture rob mayoff · Jul 11, 2012

First, you need to enforce the limit before you change the view's center. Your code changes the view's center before checking if the new center is out of bounds.

Second, you need to use the correct C operators for testing the Y coordinate. The = operator is assignment. The == operator tests for equality, but you don't want to use that either.

Third, you probably don't want to reset the recognizer's translation if the new center is out-of-bounds. Resetting the translation when the drag goes out of bounds will disconnect the user's finger from the view he's dragging.

You probably want something like this:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];

    // Figure out where the user is trying to drag the view.
    CGPoint newCenter = CGPointMake(self.view.bounds.size.width / 2,
        recognizer.view.center.y + translation.y);

    // See if the new position is in bounds.
    if (newCenter.y >= 160 && newCenter.y <= 300) {
        recognizer.view.center = newCenter;
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
}