FDFullscreenPopGesture分析

简介:

FDFullscreenPopGesture 用来解决, 横滑返回的问题。 iOS7.0之后Apple 提供了边缘的横滑返回的功能,但并没有我们需要的横向滑动文章的正文来返回到上一页的功能。 FDFullscreenPopGesture就是用来解决这个问题的。

使用:

FDFullscreenPopGesture的使用非常简单, 只需要 cocopod 进来就可以了, 默认是给所有的ViewController添加对应的横划支持。

pod 'FDFullscreenPopGesture', '1.1'

关闭指定页面的横划支持:

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

原理:

源码作者参考了一位同事的思想, 通过KVC, Runtime等方法,

  1. 系统中为每一个NavigationController默认有一个interactivePopGestureRecognizer。
  2. 自定义的手势直接添加到interactivePopGestureRecognizer对应的View上。
  3. interactivePopGestureRecognizer会操作一个指定的target , action “handleNavigationTransition”, 通过Runtime动态获取到指定的target, 及action添加到自定义的手势上。
  4. 然后把系统的interactivePopGestureRecognizer 设置为禁用方式。

Detail:
1 监控到系统中为每一个NavigationController默认有一个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对应的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动态获取到指定的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 然后把系统的interactivePopGestureRecognizer 设置为禁用方式。

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

源码实现特点:

待续...

参考:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,756评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 26,658评论 7 249
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,433评论 25 708
  • 来济南后,我有三次买房冲动,因为我手里只有10万块钱,偶尔上下浮动一下,多的时候有15万,少的时候有9万多,如果认...
    牛哥语录阅读 449评论 0 2
  • 过去的我已经死了 快乐似乎也不属于我 就连跟别人搭讪的勇气也没有了 我在黑暗中听着楼下小朋友嘻哈欢快的声音 那似乎...
    我是OK阅读 237评论 0 0

友情链接更多精彩内容