問題:
在某應(yīng)用中,DetailViewController 是從 HomeViewController push進(jìn)去的,使用到 github 上開源的 FDFullscreenPopGesture 做手勢返回,但是在 DetailViewController 中手勢返回并未生效。
簡單分析:
看了下頁面的內(nèi)容,UIViewController 中是有放了一個(gè) UIScrollView。操作過程中,在手指向右滑動(dòng),UIScrollView 滑動(dòng)到最左邊的時(shí)候,按照期望應(yīng)該是滑動(dòng)返回手勢生效。實(shí)際上 UIScrollView 的 PanGestureRecognizer 與 ViewController 的 fd_fullscreenPopGestureRecognizer 產(chǎn)生了沖突,兩者都是 Pan 手勢,到 UIScrollView 這一層的時(shí)候,就沒有繼續(xù)往下方繼續(xù)傳遞了,導(dǎo)致滑動(dòng)返回手勢失效。那是不是當(dāng) UIScrollView 滑動(dòng)到了最左邊(ContentOffset 的 x 為 0)的時(shí)候,繼續(xù)讓 ViewController 的 fd_fullscreenPopGestureRecognizer 繼續(xù)生效就可以了?試一試吧!
新建一個(gè) JJSScrollView 繼承 UIScrollView,重寫 gestureRecognizer:(UIGestureRecognizer)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer)otherGestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// 判斷 otherGestureRecognizer 是不是系統(tǒng) POP 手勢
if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
// 判斷 POP 手勢的狀態(tài)是 begin 還是 fail,同時(shí)判斷 scrollView 的 ContentOffset.x 是不是在最左邊
if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
return YES;
}
}
return NO;
}
結(jié)果:
將 UIViewController 中的 UIScrollView 改為自定義的 JJSScrolView,重新運(yùn)行應(yīng)用?;瑒?dòng) UIScrollView 到最左側(cè),繼續(xù)向左滑動(dòng),返回手勢正常。問題解決!
寫在最后
歡迎大家加我好友,一起探討 iOS 開發(fā)相關(guān)的知識,如果你在開發(fā)過程中遇到什么 bug,也可以發(fā)給我一起解決。