界面结构是这样的
实现效果是兼容底部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)】)进行侧滑事件判断处理。