简介:
FDFullscreenPopGesture 用来解决, 横滑返回的问题。 iOS7.0之后Apple 提供了边缘的横滑返回的功能,但并没有我们需要的横向滑动文章的正文来返回到上一页的功能。 FDFullscreenPopGesture就是用来解决这个问题的。
使用:
FDFullscreenPopGesture的使用非常简单, 只需要 cocopod 进来就可以了, 默认是给所有的ViewController添加对应的横划支持。
pod 'FDFullscreenPopGesture', '1.1'
关闭指定页面的横划支持:
navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
viewController.fd_interactivePopDisabled = YES;
原理:
源码作者参考了一位同事的思想, 通过KVC, Runtime等方法,
- 系统中为每一个NavigationController默认有一个interactivePopGestureRecognizer。
- 自定义的手势直接添加到interactivePopGestureRecognizer对应的View上。
- interactivePopGestureRecognizer会操作一个指定的target , action “handleNavigationTransition”, 通过Runtime动态获取到指定的target, 及action添加到自定义的手势上。
- 然后把系统的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