簡介:
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等方法,
- 系統(tǒng)中為每一個NavigationController默認(rèn)有一個interactivePopGestureRecognizer。
- 自定義的手勢直接添加到interactivePopGestureRecognizer對應(yīng)的View上。
- interactivePopGestureRecognizer會操作一個指定的target , action “handleNavigationTransition”, 通過Runtime動態(tài)獲取到指定的target, 及action添加到自定義的手勢上。
- 然后把系統(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