看到現(xiàn)在app 中越來越流行的手勢滑動(dòng)實(shí)現(xiàn)pop效果,心里很是癢癢躍躍欲試,經(jīng)過多方查看資料,終于在簡書上找到了詳細(xì)的實(shí)現(xiàn)方案:
經(jīng)過修改實(shí)現(xiàn)了簡單的(自定義導(dǎo)航控制器)手勢滑動(dòng)實(shí)現(xiàn)pop返回效果:
1.先打印了導(dǎo)航控制器的interactivePopGestureRecognizer,log結(jié)果如下:
<UIScreenEdgePanGestureRecognizer: 0x7fe5f1579ce0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fe5f1567ea0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition
2.自定義UipanGestureRecognizer,移花接木到該手勢的target和action;action好處理,但是target的活得用runtime時(shí)可以拿到,但過程略復(fù)雜,
打印interactivePopGestureRecognizer.delegate;發(fā)現(xiàn)正好也是_UINavigationInteractiveTransition,所以問題解決:
id target = self.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
3.測試發(fā)現(xiàn)如果當(dāng)前導(dǎo)航控制器的自控制器只有一個(gè)的時(shí)候,拖拽手勢會發(fā)生bug,這時(shí)就需要腹瀉pan手勢的
-(BOOL)gestureRecognizerShouldBegin:來決定手勢操作是否起作用:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return self.childViewControllers.count==1?NO :YES;
}
4.附上github上forkingdog的更加完美的分類實(shí)現(xiàn)方法:
https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
TIP:相關(guān)bug小提示,如果自定了導(dǎo)航控制器的自控制器的leftBarButtonItem,可能會引發(fā)邊緣滑動(dòng)pop效果的失靈,是由于
self.interactivePopGestureRecognizer.delegate被滯空的原因
新聞熱點(diǎn)
疑難解答
圖片精選