tiltem
tiltem

Reputation: 5032

Pass Touches from ScrollView to CollectionView below

I have following view hierarchy:

UIView - root

-UICollectionView

-UIScrollView

BOTH are subviews of root view not of each other.

I want scrollview to only scroll vertically and collectionview to only scroll horizontally.

The problem I am having is getting the collection view to receive touches as it is underneath the scrollview.

Read other SO questions but was not able to find solution. Have tried overriding touches event methods in subclass of scroll view, but no luck.

Upvotes: 4

Views: 1619

Answers (2)

hatfinch
hatfinch

Reputation: 3095

Put an extra UIScrollView (1) above your existing UIScrollView (2) and UICollectionView (3), and set its delegate to an object that has references to (2) and (3) (e.g. your parent view or view controller). Implement the following method in that delegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
    _scrollView2.contentOffset = CGPointMake(0.0, scrollView1.contentOffset.y);
    _collectionView3.contentOffset = CGPointMake(scrollView1.contentOffset.x, 0.0);
}

(Don't forget to set _scrollView1.contentSize correctly.)

Upvotes: 0

Yas Tabasam
Yas Tabasam

Reputation: 10625

An easiest fix would be to,

1) add UICollectionView to the UIScrollView,

2) Pin UICollectionView's left, right, top, bottom to UIScrollView, and

3) Pin UIScrollView's left, right, top, bottom to it's parent `UIView', if it has got one!

This way, they will both automatically know who needs to respond to which scroll events depending upon their contents.

Upvotes: 1

Related Questions