FDFullscreenPopGesture分析

簡介:

FDFullscreenPopGesture 用來解決, 橫滑返回的問題。 iOS7.0之后Apple 提供了邊緣的橫滑返回的功能,但并沒有我們需要的橫向滑動文章的正文來返回到上一頁的功能。 FDFullscreenPopGesture就是用來解決這個問題的。

使用:

FDFullscreenPopGesture的使用非常簡單, 只需要 cocopod 進(jìn)來就可以了, 默認(rèn)是給所有的ViewController添加對應(yīng)的橫劃支持。

pod 'FDFullscreenPopGesture', '1.1'

關(guān)閉指定頁面的橫劃支持:

navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
viewController.fd_interactivePopDisabled = YES;

原理:

源碼作者參考了一位同事的思想, 通過KVC, Runtime等方法,

  1. 系統(tǒng)中為每一個NavigationController默認(rèn)有一個interactivePopGestureRecognizer。
  2. 自定義的手勢直接添加到interactivePopGestureRecognizer對應(yīng)的View上。
  3. interactivePopGestureRecognizer會操作一個指定的target , action “handleNavigationTransition”, 通過Runtime動態(tài)獲取到指定的target, 及action添加到自定義的手勢上。
  4. 然后把系統(tǒng)的interactivePopGestureRecognizer 設(shè)置為禁用方式。

Detail:
1 監(jiān)控到系統(tǒng)中為每一個NavigationController默認(rèn)有一個interactivePopGestureRecognizer。

interactivePopGestureRecognizer Property
The gesture recognizer responsible for popping the top view controller off the navigation stack. (read-only)
Declaration
SWIFT
var interactivePopGestureRecognizer: UIGestureRecognizer? { get }
OBJECTIVE-C
@property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer
Discussion
The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack. You can use this property to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event. 
Availability
Available in iOS 7.0 and later.
See Also
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

2 自定義的手勢直接添加到interactivePopGestureRecognizer對應(yīng)的View上。

 if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.fd_fullscreenPopGestureRecognizer]) {       
        // Add our own gesture recognizer to where the onboard screen edge pan gesture recognizer is attached to.
        [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.fd_fullscreenPopGestureRecognizer];

3 interactivePopGestureRecognizer會操作一個指定的target , action “handleNavigationTransition”, 通過Runtime動態(tài)獲取到指定的target, 及action添加到自定義的手勢上。

         // Forward the gesture events to the private handler of the onboard gesture recognizer.
        NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
        id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
        SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
        self.fd_fullscreenPopGestureRecognizer.delegate = self.fd_popGestureRecognizerDelegate;
        [self.fd_fullscreenPopGestureRecognizer addTarget:internalTarget action:internalAction];

4 然后把系統(tǒng)的interactivePopGestureRecognizer 設(shè)置為禁用方式。

      // Disable the onboard gesture recognizer.
        self.interactivePopGestureRecognizer.enabled = NO;

源碼實(shí)現(xiàn)特點(diǎn):

待續(xù)...

參考:

https://github.com/forkingdog/FDFullscreenPopGesture
http://www.itdecent.cn/p/d39f7d22db6c

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

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,296評論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,091評論 7 249
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評論 25 709
  • 來濟(jì)南后,我有三次買房沖動,因為我手里只有10萬塊錢,偶爾上下浮動一下,多的時候有15萬,少的時候有9萬多,如果認(rèn)...
    牛哥語錄閱讀 409評論 0 2
  • 過去的我已經(jīng)死了 快樂似乎也不屬于我 就連跟別人搭訕的勇氣也沒有了 我在黑暗中聽著樓下小朋友嘻哈歡快的聲音 那似乎...
    我是OK閱讀 226評論 0 0

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