解决问题:
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.panGestureRecognizer
if (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边缘时,就会触发侧滑返回