美文网首页OC-开发案例收集
解决 UICollectionView 横向滑动时,系统侧滑返回

解决 UICollectionView 横向滑动时,系统侧滑返回

作者: tuesone | 来源:发表于2020-12-03 10:43 被阅读0次

    获取向右滑动时和 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
    

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

    相关文章

      网友评论

        本文标题:解决 UICollectionView 横向滑动时,系统侧滑返回

        本文链接:https://www.haomeiwen.com/subject/hefmwktx.html