本文主要是介绍iOS解决UICollectionView下嵌套UITableView多个列表时侧滑返回失效及cell侧滑删除失效的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解决问题:
UICollectionView下嵌套UITableView作为多个列表时侧滑返回失效及cell侧滑删除失效的问题
继承自UICollectionView:
@implementation XYCollectionView
// 是否允许同时支持多个手势,默认是不支持多个手势
// 返回yes表示支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {if (gestureRecognizer.view == self) {if (self.contentOffset.x <= 0 && gestureRecognizer.state != UIGestureRecognizerStatePossible) {return YES;}}return NO;
}
// 每次触摸屏幕时保证collectionView第一时间可以响应滚动
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {self.scrollEnabled = YES;return [super hitTest:point withEvent:event];
}
@end
设置下面这个是为了触发侧滑返回时collectionView不再去滚动cell, 在CollectionView的代理方法中,根据collectionView的contentOffse让其是否可以滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIPanGestureRecognizer *pan = (UICollectionView *)scrollView.panGestureRecognizerif (pan.view == scrollView) {if (_switchState == 0 && _downloadingArr.count > 0) {for (NSInteger i = 0; i < _downloadingArr.count; ++i) {NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];// 此处是为了解决cell左滑删除失效的问题if (cell && [cell isKindOfClass:[DownloadingCell class]]) {CGPoint location = [pan locationInView: scrollView];if (location.y > 0 && location.y < cell.frame.size.height*(i+1)) {scrollView.scrollEnabled = NO;}}}}// 此处是为了解决滑动到第一个cell左侧边缘时侧滑返回失效问题if (scrollView .contentOffset.x < 0) {scrollView.scrollEnabled = NO;} else {scrollView.scrollEnabled = YES;}}}
最终效果:每次又滑到第一个cell边缘时,就会触发侧滑返回
这篇关于iOS解决UICollectionView下嵌套UITableView多个列表时侧滑返回失效及cell侧滑删除失效的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!