0

I'am facing a little problem with my App that has a bit complicated structure :

So first things first the views hirearchy :

|- UICollectionView (horizontal scrolling)
 -> UICollectionViewCell
  |-> UIScrollView A (PagingEnabled = YES, vertical scrolling only)
    |-> UIImageView = page 1
    |-> UIScrollView B (PagingEnabled = NO, vertical scrolling only) = page 2
      |-> UIView (content...)

The problem is that when I scroll on UIScrollView A to make page 2 appear, the first scroll to the see the bottom on UIScrollView B is ignored, no scroll at all, but the second one has the right behaviour.

I think that it's a kind of "focusing" problem, but I can't figure out how to make this work properly.

Any clue on how to give this first touch on page 2 to the right UIScrollView ?

Dulgan
  • 6,674
  • 3
  • 41
  • 46

1 Answers1

1

Have you tried hitTest:withEvent: method? This method helps you choose which view should receive the touch.

For example, you could subclass UIScrollView and set your UIScrollView A's class the new class you just created. Adding the code below to this custom UIScrollView class will let you deliver the touch to it's child view's first

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView *subView in self.subviews) {
        // Convert the point to the subView's coordinate system.
        CGPoint pointForTargetView = [subView convertPoint:point fromView:self];

        if (CGRectContainsPoint(subView.bounds, pointForTargetView)) {
            // Calling subView's hitTest:withEvent get the right view
            return [subView hitTest:pointForTargetView withEvent:event];
        }
    }

    UIView *hitView = [super hitTest:point withEvent:event];

//  // Uncomment if you want to make the view 'transparent' to touches:
//  // meaning the parts of the view without any subview will deliver the touch
//  // to the views 'behind' it
//  if (hitView == self) {
//      return nil;
//  }

    return hitView;
}

For more detailed info please check WWDC - Advanced Scrollviews and Touch Handling Techniques (around 15:00 is where this subject starts).

Also, thanks to Tim Arnold for the clue.

Community
  • 1
  • 1
Islam
  • 3,654
  • 3
  • 30
  • 40
  • Thanks for your answer and links, I made it using another gesture recognizer, recognizing the first gesture only and scrolling programmatically by setting the content offset. You code is definitely a better way to perform this. – Dulgan Dec 04 '14 at 13:33
  • No problem, I hope it's going to help others in the future. Cheers – Islam Dec 04 '14 at 15:44