1、 UIScrollView
里面嵌套两个tableView
,右边一个tableView
又要实现左滑删除功能,写好左滑删除的代理方法,准备调试... 发现tableView
左滑失效了,调试发现原来是UIScrollView
的滑动手势和tableView
的左滑手势冲突了。
2、写了一个继承UISrollView
类 MyScrollView
,在里面重写
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSLog(@"_____%@______other:%@",gestureRecognizer,otherGestureRecognizer);
return NO;
}
(上面方法是UIGestureRecognizerDelegate有两个没公开的函数之一,只要重载了就会被调用,是可以通过appstore审核的)
调试输出发现 return NO
会忽略otherGestureRecognizer
手势(被拦截) ;return YES
则会调用 otherGestureRecognizer
的方法(不拦截);但是需求是cell
左滑的手势可以执行,而不是所有的手势;单纯的return YES
会造成UISrollView
无法左右滑动。
3、知道上面的的问题,那我们只需要判断 如果otherGestureRecognizer
是cell
左滑的手势 return yes
即可,其他return no
。 通过调试打印发现
___<UIScrollViewPanGestureRecognizer: 0x138081870; state = Possible; delaysTouchesEnded = NO; view = <ANTScrollView 0x1370e9400>; target= <(action=handlePan:, target=<ANTScrollView 0x1370e9400>)>; must-fail = {
<UIScrollViewPagingSwipeGestureRecognizer: 0x138014430; state = Failed; view = <ANTScrollView 0x1370e9400>; target= <(action=_handleSwipe:, target=<ANTScrollView 0x1370e9400>)>>
}>______other:<UIScrollViewPanGestureRecognizer: 0x1380175c0; state = Possible; cancelsTouchesInView = NO; delaysTouchesEnded = NO; view = <UITableViewWrapperView 0x1370cec00>; targets= <(
"(action=handlePan:, target=<UITableViewWrapperView 0x1370cec00>)",
"(action=handleSwipeBeginning:, target=<UITableViewWrapperView 0x1370cec00>)"
)>>
2016-07-14 14:53:32.463 antQueen[7614:3034218] ___<UIScrollViewPanGestureRecognizer: 0x138081870; state = Possible; delaysTouchesEnded = NO; view = <ANTScrollView 0x1370e9400>; target= <(action=handlePan:, target=<ANTScrollView 0x1370e9400>)>; must-fail = {
<UIScrollViewPagingSwipeGestureRecognizer: 0x138014430; state = Failed; view = <ANTScrollView 0x1370e9400>; target= <(action=_handleSwipe:, target=<ANTScrollView 0x1370e9400>)>>
}>______other:<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x13807a5a0; state = Possible; delaysTouchesBegan = YES; view = <UITableView 0x13703f000>; target= <(action=delayed:, target=<UITableView 0x13703f000>)>>
2016-07-14 14:53:32.472 antQueen[7614:3034218] ___<UIScrollViewPanGestureRecognizer: 0x138081870; state = Possible; delaysTouchesEnded = NO; view = <ANTScrollView 0x1370e9400>; target= <(action=handlePan:, target=<ANTScrollView 0x1370e9400>)>; must-fail = {
<UIScrollViewPagingSwipeGestureRecognizer: 0x138014430; state = Failed; view = <ANTScrollView 0x1370e9400>; target= <(action=_handleSwipe:, target=<ANTScrollView 0x1370e9400>)>>
}>______other:<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x136db8360; state = Possible; delaysTouchesBegan = YES; view = <ANTScrollView 0x1370e9400>; target= <(action=delayed:, target=<ANTScrollView 0x1370e9400>)>>
我们发现其中有个UITableViewWrapperView
这个就是我们要找的view
,然后我们在方法里面做个判断:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UITableViewWrapperView")]) {
return YES;
}
NSLog(@"_____%@______other:%@",gestureRecognizer,otherGestureRecognizer);
return NO;
}
大功告成...调试发现cell可以实现左滑。
通过上面的私有方法,也可以去解决UIScrollView
滑动与UINavigationVIewController
的手势返回的冲突(自己研究下吧!)