title: Panning objects on a UIScrollView
body: <p>I'm working on an app where I have several UIView objects that are subviews on a UIScrollView object. I create the subviews programmatically and place them on the scroll view according to the properties of associated objects. The user is allowed to move these subviews around on the scroll view. Usually this works, but sometimes the scrollview grabs the pan gesture.</p>
  
  <p>What I'd like to do is to suppress the scroll view gesture recognizer if the touch location is inside one of the subviews.</p>
  
  <p>I can find the scroll view gesture recognizer by looking through the scroll view's array of gesture recognizers and looking for a <code>UIScrollViewPanGestureRecognizer</code> object. I assume there can only be one.</p>
  
  <p>An idea I have is to make my view controller be a delegate of this gesture recognizer and then have the delegate suppress it if the touch is within the bounds of one of the subviews.</p>
  
  <p>Is this the best way to handle this scenario, or is there a better way?</p>
  
  <p>I've done something similar, described in my answer to my own question here.
  <a href="https://stackoverflow.com/questions/20847475/how-to-get-stepper-and-longpress-to-coexist">How to get stepper and longpress to coexist?</a></p>
  
  <p>Hmmm. Looks like it will be more difficult than I anticipated to recognize the scrollview's <code>UIScrollViewPanGestureRecognizer</code>. Any hints on doing this would be appreciated.</p>
  
  <p>My idea doesn't work. In order to code my idea, I had to make my VC be the delegate of the scrollview's pan gesture recognizer. However, when I do that, I get this error:</p>
  
  <p><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.'</code></p>
  
  <p>Here is the code I used. In <code>viewDidLoad</code> I called a method which got the scrollview's pan gesture recognizer and set self as delegate (<code>self.scrollViewPanGestureRecognizer</code> is just a property to store it):</p>
  
  <pre><code>self.scrollViewPanGestureRecognizer = [self.scrollView panGestureRecognizer];
  self.scrollViewPanGestureRecognizer.delegate = self;
  </code></pre>
  
  <p>I then implemented this delegate method:</p>
  
  <pre><code>-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  {
     //Disable touch if touch location is in a subview.
     BOOL enableGestureRecognizer = YES;
  
     if (gestureRecognizer == self.scrollViewPanGestureRecognizer) {
        CGPoint touchLocation = [touch locationInView:self.scrollView];
        for (UIView *s in self.scrollView.subviews) {
           if (CGRectContainsPoint(s.frame, touchLocation)) {
              enableGestureRecognizer = NO;
           }
        }
     }
     return enableGestureRecognizer;
  }
  </code></pre>
  
  <p>Seemed like a good idea, but it looks like I can't make my VC be the delegate.</p>
  
  <p>Just tried setting <code>scrollEnabled</code> to <code>NO</code> on the scroll view. That successfully disabled scrolling, but it did not fix the problem. Views still occasionally do not respond to gestures. Thinking that perhaps some bug caused the gesture recognizer to fall off the object, I asked the debugger to display the gesture recognizers for the problematic views. They were still there. I'm stumped.</p>
  
  <p>UPDATE: New information. I finally realized that the subviews that aren't responding are the ones on the right side of the screen. After carefully testing, it seems that this happens only in landscape orientation and only when the finger location is to the right of the right edge in portraite, i.e. 320 points. Apparently, something is not being handled property when rotating to landscape. Everything appears normal, but the gestures aren't being recognized.</p>
  
  <p>Just for grins, I decided to display the frames and bounds and content area in the method <code>viewDidLayoutSubviews</code>. What I get is:</p>
  
  <pre><code>self.view.frame             is {{0, 0}, {480, 320}} 
  self.view.bounds            is {{0, 0}, {480, 320}} 
  self.scrollView.frame       is {{0, 0}, {480, 320}} 
  self.scrollView.bounds      is {{0, 0}, {480, 320}}
  self.scrollView.contentSize is {480, 320}
  </code></pre>
  
  <p>I seem to have missed something. What else needs to be set when rotating?</p>