简体   繁体   中英

CollectionViewCell with pan gesture recognizer

I have setup my UICollectionViewCell with a UIPanGestureRecognizer that should work like the ones you often see in UITableViewCells that reveals controls underneath the content. My question should apply to both UICollectionViews and UITableViews though.

I have it working so that the horizontal pan in the cell works simultaneously with the vertical pan of the UICollectionView . This is enabled simply by implementing this delegate method of UIGestureRecognizer :

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

This allows them to work simultaneously, but it also have the side effect that, as I am panning vertically in the UICollectionView , any slight horizontal movement will trigger the gestureRecognizer in the cell that I happened to touch while starting the vertical pan and thus revealing the underlying controls. I do not want this.

Mail handles this perfectly, so that fx. a vertical pan in the UITableView , disables any horizontal pan gestures in the cells and vice versa.

Can I achieve this in a simple way by setting up my gesture recognizers correctly?

I'd like to avoid a solution where I have to manage state between the views ("Scroll view is dragging", "cell is dragging" etc).

The solution to this issue may be seen in the WWDC 2014 Video: "Advanced scroll views and touch handling techniques." in the section where they explain how you can implement dragging while scrolling using multiple touches. The idea is to disable then re-enable the gesture recognizer you don't want to run in parallel to the one that's currently working. In your case you're need to subclass the collectionView and the tableView in order to override their implementation of the pan gesture recognizer.

First:

I have it working so that the horizontal pan in the cell works simultaneously with the vertical pan of the CollectionView. This is enabled simply by implementing this delegate method of UIGestureRecognizer.

Later:

Mail handles this perfectly, so that fx. a vertical pan in the TableView, disables any horizontal pan gestures in the cells and vice versa.

You're asking for two opposite behaviours. Decide which one you want your app to implement.

If you wish the gestures to work simultaneously (bad design IMO), keep the gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer: in your code.

If you want them to work like in the Mail app (the way they normally do in a table view), remove the offending line of code.

Also, the way this is implemented in a table view is using a scroll view. So you might want to look into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM