UIScrollView嵌套UITableView,cell侧滑兼容 iOS适配

界面结构是这样的

界面结构

实现效果是兼容底部sceollView左右滑动和内部cell侧滑操作
通过scrollView的手势代理来兼容table的手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UITableViewWrapperView")]) && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint p = [otherGestureRecognizer locationInView:otherGestureRecognizer.view ];
        if (self.otherGestureRecognizerSimultaneously && p.x >= SCREEN_WIDTH-SCREEN_WIDTH/375*50) {
            return YES;
        }
    }
    return NO;
}

附带pop手势兼容

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
        if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
            return YES;
        }
    }
    return NO;
}

这是iOS11之前的做法,iOS11 的UITableView的调整感觉还满多的,自少目前遇到的情况是这样,对于cell的侧滑兼容需要做一点小改动
cell手势判断条件调整

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //UITableViewCell 自定义手势可能要在此处自行定义
    static Class kUIPanGestureRecognizer, kUITableView;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        kUIPanGestureRecognizer = [UIPanGestureRecognizer class];
        if (@available(iOS 11.0, *)) {
            kUITableView = NSClassFromString(@"UITableView");
        } else {
            kUITableView = NSClassFromString(@"UITableViewWrapperView");
        }
    });
    //self.otherGestureRecognizerSimultaneously 启用其它手势
    if (self.otherGestureRecognizerSimultaneously && [otherGestureRecognizer isKindOfClass:kUIPanGestureRecognizer] &&  [otherGestureRecognizer.view isKindOfClass:kUITableView]) {
        CGPoint p = [otherGestureRecognizer locationInView:otherGestureRecognizer.view];
        CGFloat w = CGRectGetWidth(otherGestureRecognizer.view.bounds);
        if (p.x >= w - 50) {
            return YES;
        }
//        if (p.x <= 50) {
//            return YES;
//        }
    }
    return NO;
}

这样cell的侧滑操作就能够正常兼容iOS11了。
以上代码只是粗略处理滑动冲突,需要精确可根据p的位置对对应cell(【bool CGRectContainsPoint(CGRect rect, CGPoint point)】)进行侧滑事件判断处理。

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

推荐阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,143评论 1 6
  • 转自:https://www.jianshu.com/p/10b2323f502e 1、禁止手机睡眠 [UIApp...
    aggie1024阅读 2,686评论 0 6
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    小小夕舞阅读 1,485评论 1 1
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,674评论 3 3
  • 努力去做你想做的事情,哪怕你遇到的困难,挫折,指责,不理解,看不起,这些都要忍着,等一天你开花结果了,结果会证明...
    漫步奋斗路阅读 131评论 0 0