Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A nice way of doing this without messing about with only starting to pan when you've moved a certain value is to determine which direction the gesture recognizer has translated the most in and using that. It cuts down on the amount of code you need quite a bit and works fairly well in my testing.</p> <pre><code>- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:self.superview]; return fabsf(translation.x) &gt; fabsf(translation.y) } return YES; } </code></pre> <p>And now your view sliding becomes simpler. This code allows you to drag in either direction.</p> <pre><code>- (void)drag:(UIPanGestureRecognizer *)sender { CGPoint translation = [sender translationInView:self.superview]; CGRect frame = self.upperView.frame; switch (sender.state) { case UIGestureRecognizerStateChanged: frame.origin.x = translation.x; [self.upperView setFrame:frame]; break; case UIGestureRecognizerStateBegan: case UIGestureRecognizerStateEnded: case UIGestureRecognizerStateFailed: default: break; } } </code></pre> <p>Additionally, you may want to allow your gesture recognizer to work along side others (as in, the recognizer that handles the scrolling of the table view) but the code in <code>gestureRecognizerShouldBegin:</code> seems to handle this without issue.</p> <pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } </code></pre> <p>Also note that I'm using <code>self.superview</code> instead of just <code>self</code> as you have. This is because of this line in the <code>translationInView:</code> section of the docs:</p> <blockquote> <p>The view in whose coordinate system the translation of the pan gesture should be computed. If you want to adjust a view's location to keep it under the user's finger, <strong>request the translation in that view's superview's coordinate system.</strong></p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload