UIScrollView嵌套TableView手勢沖突問題

1、 UIScrollView里面嵌套兩個(gè)tableView,右邊一個(gè)tableView 又要實(shí)現(xiàn)左滑刪除功能,寫好左滑刪除的代理方法,準(zhǔn)備調(diào)試... 發(fā)現(xiàn)tableView左滑失效了,調(diào)試發(fā)現(xiàn)原來是UIScrollView的滑動(dòng)手勢和tableView的左滑手勢沖突了。

2、寫了一個(gè)繼承UISrollViewMyScrollView,在里面重寫

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
  NSLog(@"_____%@______other:%@",gestureRecognizer,otherGestureRecognizer);
return NO;
}

(上面方法是UIGestureRecognizerDelegate有兩個(gè)沒公開的函數(shù)之一,只要重載了就會(huì)被調(diào)用,是可以通過appstore審核的)
調(diào)試輸出發(fā)現(xiàn) return NO會(huì)忽略otherGestureRecognizer 手勢(被攔截) ;return YES則會(huì)調(diào)用 otherGestureRecognizer的方法(不攔截);但是需求是cell左滑的手勢可以執(zhí)行,而不是所有的手勢;單純的return YES會(huì)造成UISrollView 無法左右滑動(dòng)。

3、知道上面的的問題,那我們只需要判斷 如果otherGestureRecognizercell左滑的手勢 return yes 即可,其他return no。 通過調(diào)試打印發(fā)現(xiàn)

___<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>)>>

我們發(fā)現(xiàn)其中有個(gè)UITableViewWrapperView這個(gè)就是我們要找的view,然后我們?cè)诜椒ɡ锩孀鰝€(gè)判斷:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UITableViewWrapperView")]) {
        return YES;
    }
    NSLog(@"_____%@______other:%@",gestureRecognizer,otherGestureRecognizer);
    return NO;
}

大功告成...調(diào)試發(fā)現(xiàn)cell可以實(shí)現(xiàn)左滑。

通過上面的私有方法,也可以去解決UIScrollView滑動(dòng)與UINavigationVIewController的手勢返回的沖突(自己研究下吧?。?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容