解决 UICollectionView 横向滑动时,系统侧滑返回功能冲突

获取向右滑动时和 UICollectionView的横向滑动冲突,在手势按住 cell 之间的空隙是会出现 页面返回,解决思路如下

1.设置 UICollectionView.backgroundColor 不能为空或者clearColor。
UICollectionView.backgroundColor = [UIColor whiteColor]; 
2.如果 UICollectionView装在tableView中也要设置其 父视图的backgroundColor 不能为空或者clearColor。
UITableView.backgroundColor = [UIColor whiteColor]; 
3.由于UICollectionView继承自UIScrollView ,在UIScrollView做手势返回判断处理,添加分类 UIScrollView+TZGestureRecognizer.h,

分类直接放入项目即可生效。
UIScrollView+TZGestureRecognizer.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIScrollView (TZGestureRecognizer)

@end

NS_ASSUME_NONNULL_END

UIScrollView+TZGestureRecognizer.m

#import "UIScrollView+TZGestureRecognizer.h"


@implementation UIScrollView (TZGestureRecognizer)

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
    
    // 首先判断otherGestureRecognizer是不是系统pop手势
    if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
        
        // 再判断系统手势的state是began还是fail,
        // 同时判断scrollView的位置是不是正好在最左边
        if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
            
            return YES;
        }
    }
    
    return NO;
}

@end

笔记记录下,目前已知解决最为简便的方法,有更好的解决方案请留言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容