WKWebView? allowsBackForwardNavigationGestures屬性作用是設(shè)置手勢(shì)前進(jìn)后退,若需要單獨(dú)控制前進(jìn)或者后退則需要對(duì)該屬性進(jìn)行分解。
分析發(fā)現(xiàn)WKWebView的前進(jìn)后退是由兩個(gè)UIScreenEdgePanGestureRecognizer (UIRectEdgeLeft、UIRectEdgeRigth) 手勢(shì)來(lái)控制的,在allowsBackForwardNavigationGestures SET 操作時(shí)候進(jìn)行了手勢(shì)的增加刪除(刪除手勢(shì)并且手勢(shì)的target會(huì)被置nil)。并不是單純的enabled設(shè)置。
找到手勢(shì)就好辦了,創(chuàng)建一個(gè)AYWKWebView 繼承 WKWebView,
用NS_UNAVAILABLE宏禁用allowsBackForwardNavigationGestures,并且定義兩個(gè)分解屬性
在WebView init時(shí)候調(diào)用super.allowsBackForwardNavigationGestures = YES; (self.allowsBackForwardNavigationGestures已經(jīng)被禁用),重寫-[addGestureRecognizer:]方法,UIScreenEdgePanGestureRecognizer手勢(shì)進(jìn)行強(qiáng)引用,把新添加的手勢(shì)禁用掉,
然后在allowsBackNavigationGestures和allowsForwardNavigationGestures的 SET 方法設(shè)置對(duì)應(yīng)手勢(shì)的enabled屬性,
重寫-[addGestureRecognizer:]為避免其它UIScreenEdgePanGestureRecognizer手勢(shì)干擾,增加一個(gè)allowsBackNavigationGesturesSet屬性標(biāo)記是否是由于allowsBackNavigationGestures設(shè)置導(dǎo)致,在[supersetAllowsBackForwardNavigationGestures:]方法執(zhí)行前后進(jìn)行allowsBackNavigationGesturesSet開關(guān)控制
然后改寫一下-[addGestureRecognizer:]方法
這樣就實(shí)現(xiàn)了allowsBackForwardNavigationGestures屬性的分解