title: How to steal touches from UIScrollView?
body: <p>Today on my creative time I did some quite comprehensive research on how to steal touches from a UIScrollView and send them instantly to a specific subview, while maintaining the default behavior for the rest of the scroll view. Consider having a UIPickerView inside of a UITableView. The default behavior is that if you drag your finger over the picker view, the scroll view will scroll and the picker view will remain unchanged.</p>
  
  <p>The first thing I tried was to override</p>
  
  <pre><code>- (BOOL)touchesShouldCancelInContentView:(UIView *)view
  </code></pre>
  
  <p>and simply not allow the UIScrollView to cancel touches inside the picker view. This works, but it has an unpleasant side effect. You would like the picker view to respond immediately and thus you will have to set <code>delaysContentTouches</code> to NO. The problem is that you don't want the rest of the table view to respond immediately, because if it does the table view cell will always get highlighted for a few milliseconds before the scrolling starts.</p>
  
  <p>The second thing I tried was to override</p>
  
  <pre><code>- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  </code></pre>
  
  <p>because I had read that the scroll view always returns itself, so that it will "steal" the touches from its subviews and later send them to the subview if they weren't of interest to the scroll view. However, this isn't true anymore. UIScrollView's default implementation of hitTest:withEvent: actually returns the subview that should receive the touch. Instead it uses gesture recognizers to intercept the touches.</p>
  
  <p>So the third thing I attempted was to implement my own gesture recognizer and cause it to fail if the touch was outside of the picker view and otherwise succeed. Then I set all the scroll view's gesture recognizers to fail unless my gesture recognizer failed using the following code:</p>
  
  <pre><code>for (UIGestureRecognizer * gestureRecognizer in self.tableView.gestureRecognizers)
  {
      [gestureRecognizer requireGestureRecognizerToFail:myRecognizer];
  }
  </code></pre>
  
  <p>This does in fact steal the touches from the scroll view, but the picker view never receives them. So I though maybe I could just send all the touches that my gesture recognizer receives using this code:</p>
  
  <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
      for (UITouch *touch in touches)
          [touch.view touchesBegan:touches withEvent:event];
  }
  </code></pre>
  
  <p>The above code is a simplified version. I also make sure that the view is a picker view (or one of it's subviews) and set the appropriate state for the gesture recognizer as I mentioned above. I also did the same for canceled, ended and moved. However, the picker view was still not responding.</p>
  
  <p>I also tried one last thing before returning to my regular work. During my extensive googling I read that nested UIScrollViews just magically worked since 3.x, so I tried putting my picker view inside a nested UIScrollView and set the following properties on it:</p>
  
  <pre><code>scrollView.delaysContentTouches = NO;
  scrollView.canCancelContentTouches = NO;
  </code></pre>
  
  <p>As one would expect the outer scroll view didn't treat the inner scroll view any different than it treated the picker view, so the inner scroll view did not receive the touches. I thought that it was a long shot, but it was simple enough to implement, so I thought it was worth to give it a shot.</p>
  
  <p>What I know is that UIScrollView has a gesture recognizer named <code>UIScrollViewDelayedTouchesBeganGestureRecognizer</code> that intercepts the touches and sends them to the appropriate subview after 150 (?) ms. I'm thinking that I should be able to write a similar recognizer that causes the scroll view's default recognizers to fail and instead of delaying the touches immediately sends them to the picker view. So if anyone knows how to write such a recognizer please let me know and if you have any other solution to the problem, you're very welcome share that as well.</p>
  
  <p>Thank you for reading through the whole question and even if you don't know the answer you could still upvote the question so that it gets more attention (hopefully from someone that can answer it). Thanks! :)</p>