Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

uicollectionview - UICollectionViewCell with UIScrollView cancels didSelectItemAtIndexPath

I have a UICollectionView that horizontally scrolls to show one UICollectionViewCell at a time. Each UICollectionViewCell the has a vertically scrolling UIScrollView as subview for scrolling the contents of the cell. It is only 90 % or so of the inner part of the UICollectionViewCell that is covered by the UIScrollView - i.e. the outer frame of the cell is not covered by this.

It turns out that the part of the UICollectionViewCell that is covered by the UIScrollViewcancels the UICollectionView delegate didSelectItemAtIndexPath. Thus when a simple tap happens within the UIScrollView this method is not invoked, whereas if the tap happens on the outer part of the cell, i.e. outside the UIScrollView, this method is invoked.

Any suggestions as to how to achieve a setup where it is possible to invoke the didSelectItemAtIndexPath method even when the tap happens within the UIScrollView?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found that the most effective approach is to steal the panGestureRecognizer, exposed by UIScrollView and disable userInteraction on the scrollView. That way, you get the behavior of the scrollview but maintain the interaction on the collection view. On your UICollectionViewCell subclass:

self.scrollView.userInteractionEnabled = NO;
[self.contentView addGestureRecognizer:self.scrollView.panGestureRecognizer];

This is a method Apple recommends and demonstrates in WWDC 2014 session 235 (Advanced Scrollviews and Touch Handling Techniques)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...