title: Setting position limits for a draggable UIView when using UIPanGestureRecognizer
body: <p>This question has some relation to this post I made recently: <a href="https://stackoverflow.com/questions/9308246/drag-a-uiview-part-way-then-it-moves-on-its-own/9308326#comment11741380_9308326">Drag a UIView part-way, then it moves on its own</a></p>
  
  <p>In short, I have a draggable UI view partly visible at the bottom of the window.  There is a "pull tab" that the user can use to drag the view upwards or downwards on the screen.  I have placed limits on the Y position for the upper and lower positions using this code, but I have no clue if this is the right way to do it:</p>
  
  <pre><code>- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
  {
      UIView *piece = [gestureRecognizer view];
      [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
  
      if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
      {
          CGPoint velocity = [gestureRecognizer velocityInView:[piece superview]];
          CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
  
          if(velocity.y &lt; 0)  //user is dragging the view upwards the screen
          {
              // Set the maximum Ypos as the top 1/3rd of the screen
              CGFloat maxYPos = self.view.frame.size.height/3;
              if(blogTextView.frame.origin.y &gt;= maxYPos)
              {
                  [piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
                  [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
              }
          }
          else
          {
              // User is dragging the view downwards the screen
              // Set the lowest Y position to be 420
              if(blogTextView.frame.origin.y &lt;= 420) //size of remaining view
              {
                  [piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
                  [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
              }
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>I think this looks ugly, but it works if the user is dragging the view slowly.  The problem is if the user drags the view very rapidly upward or downward the screen, then the view can be pulled beyond the limits I put on the Y position.  For example, in the default state with the draggable view on the bottom, I was able to drag the view in a snap-like speed all the way to over the navigation bar!  </p>
  
  <p>Is there a correct way to set the limits of how far a view can be dragged and will be dealt with correctly in this gestureRecognizer callback?</p>
  
  <p>For reference, here are pics of the test app:</p>
  
  <p><a href="http://dl.dropbox.com/u/762437/screens/default.jpg" rel="nofollow noreferrer">Default screen with draggable view at the bottom and the pull tab</a></p>
  
  <p><a href="http://dl.dropbox.com/u/762437/screens/dragged-position.jpg" rel="nofollow noreferrer">View dragged up to its correct max Y position</a></p>
  
  <p>Thank you!</p>