title: iPhone: setting required length of swipe gesture
body: <p>I was wondering if there is an easy method to set the minimum swipe length, i.e. the length in pixel the user needs to swipe so that the gesture is recognised as a swipe.</p>
  
  <p>I noticed that the normal swipe can be quite unresponsive (as compared to swiping photos in your photo library for instance).</p>
  
  <p>This is the normal way, but I would like to reduce the required length of the swipe:</p>
  
  <pre><code>    - (void)viewDidLoad 
      {
  
  
      // SWIPING GESTURES:
  
      UISwipeGestureRecognizer *swipeLeftRecognizer;
      swipeLeftRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundLeftSwipe:)];
      swipeLeftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
      //swipeRecognizer.numberOfTouchesRequired=1;
      [self.view addGestureRecognizer:swipeLeftRecognizer];
      [swipeLeftRecognizer release];
  
      UISwipeGestureRecognizer *swipeRightRecognizer;
      swipeRightRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(foundRightSwipe:)];
      swipeRightRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
      //swipeRecognizer.numberOfTouchesRequired=1;
      [self.view addGestureRecognizer:swipeRightRecognizer];
      [swipeRightRecognizer release];
  
  
      [super viewDidLoad];
  }
  
  #pragma mark -
  #pragma mark Swipes
  
  - (void)foundLeftSwipe:(UISwipeGestureRecognizer *)recognizer {
      // do something
  }
  
  - (void)foundRightSwipe:(UISwipeGestureRecognizer *)recognizer {
      // do something
      }
  </code></pre>
  
  <p>I remember that there is a way to get the pixel start position and end position and then compare the two, but was just wondering if there is a simpler method, i.e. by simply defining a value for the minimum required swipe length in the code I have here.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>This is how I recoded the whole thing:</p>
  
  <pre><code>    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      gestureStartPoint = [touch locationInView:self.view];
  }
  
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      CGPoint currentPosition = [touch locationInView:self.view];
  
      CGFloat deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right
      CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down
  
      CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
      CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive
  
      if (deltaX &gt;= kMinimumGestureLength &amp;&amp; deltaY &lt;= kMaximumVariance) {
          if (deltaXX &gt; 0) {
                  label.text = @"Horizontal Left swipe detected";
                  [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
              }
          else {
                  label.text = @"Horizontal Right swipe detected";
                  [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
              }
  
      }
  
  
      if (deltaY &gt;= kMinimumGestureLength &amp;&amp; deltaX &lt;= kMaximumVariance) {
          if (deltaYY &gt; 0) {
              label.text = @"Vertical up swipe detected";
              [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
              }
          else {
              label.text = @"Vertical down swipe detected";
              [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
          }
  
  
      }
  
  
  
  }
  </code></pre>