IOS-FDFullscreenPopGesture的使用


https://www.jianshu.com/p/29e456ffa831

2017.12.25 20:39* 字數 484 閱讀 2057評論 1

先貼上大神的blog 以示敬意 http://blog.sunnyxx.com/2015/06/07/fullscreen-pop-gesture/

改善后的demo地址   https://github.com/1570383140/FDFullscreenPopGesture

不得不說這真的是一個很牛逼的庫。
首先這是一個對全局都能起作用的庫,他有一些默認的操作。意思就是說你只要導入了就能夠起作用。

默認起的作用就是全屏的返回手勢

說真的,賊好用,對於大冬天不願意拿出2只手但是屏幕又大的同學,真的是交互上的絕對性勝利。而且這個實現簡直真的可以像大神說的 絲滑來形容。緣 妙不可言

導航欄的改變

以 A push B 作介紹

想要控制導航欄的顯示或者隱藏,Apple對導航欄的API設計上總是不盡人意。
唉,UINavigationController和UIViewController總是2個不相同的控制器,並且他們的關系其實並沒有達到誰控制誰。

如果需求是需要Apush后將B隱藏,我們在設置的時候一般會

  • 1、

     

    - (void)viewWillAppear:(BOOL)animated {

        [super viewWillAppear:animated];

        [self.navigationController setNavigationBarHidden:YES animated:YES];

    }

     

    - (void)viewWillDisappear:(BOOL)animated {

        [super viewWillDisappear:animated];

        [self.navigationController setNavigationBarHidden:NO animated:YES];

    }

     

     

    或者2、

     

    //要設置代理UINavigationControllerDelegate

    // 將要顯示控制器

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

        // 判斷要顯示的控制器是否是自己

        BOOL isShowHomePage = [viewController isKindOfClass:[self class]];

        [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];

    }

     

但是如果碰到喪心病狂的老板,他不要A也不要B,但是就要Push。

如果還按照以上寫法,在用手勢返回或者pop返回的時候,A界面會viewWillAppear執行。導致在滑動的過程中我們可以看到A界面導航欄的存在。這對強迫症來說完全不能忍,這時候FDFullscreenPopGesture就完全的解決了我們的問題。重要的是代碼非常簡潔,一句話。在所需要隱藏導航欄的界面寫上

- (void)viewDidLoad

    [super viewDidLoad];

    self.navigationController.fd_prefersNavigationBarHidden = YES;

}

 

或者喜歡重載的寫法也行:

- (BOOL)fd_prefersNavigationBarHidden {

    return YES;

}

 

解決FDFullscreenPopGesture連續多個頁面隱藏導航欄的BUG

0.122018.01.02 23:18:50字數 413閱讀 405

下面的方法可以用FDFullscreenPopGesture實現相鄰頁面的導航欄任意交替隱藏和顯示(主要解決連續多個頁面隱藏導航欄出現的BUG)

實現方法:

  • 在所有需要隱藏導航欄的頁面加上如下代碼
@property (nonatomic, assign) BOOL previousNaviBarShow; 
#import "UINavigationController+FDFullscreenPopGesture.h" - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 在所有需要隱藏導航欄的頁面加上這兩行代碼,所有需要顯示導航欄的頁面不做任何操作即可 self.fd_prefersNavigationBarHidden = YES; [self.navigationController setNavigationBarHidden:YES animated:self.previousNaviBarShow]; } 
  • 在所有 由(顯示導航欄頁面)推出(隱藏導航欄頁面)的地方,把要推出頁面的previousNaviBarShow置為YES

   多個界面交替隱藏導航欄的    demo地址

注意,寫了fd_prefersNavigationBarHidden就不能再去寫關於viewWillAppear/viewWillDisappear/等一系列廢操作了。因為這一句話已經囊括隱藏導航欄的代碼了

 

 

 

使用FDFullscreenPopGesture遇到的坑以及解決方法

https://www.jianshu.com/p/bbc2305d83e2

最近需要給APP添加手勢左滑返回功能,使用了框架FDFullscreenPopGesture,把使用過程中遇到的坑以及解決方法記錄下

2018-05-03 更新------

當使用相機進行拍攝的時候,頁面的上方看不見了,閃光燈功能使用不了,所以添加了如下判斷:

    // 設置導航的顯示/隱藏

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        UIViewController *vc = [self.navigationController.viewControllers lastObject];

        // 添加對相機拍攝的過濾

        if ([self.navigationController isKindOfClass:[UIImagePickerController class]]

            ) {

            UIImagePickerController *imagePickVC = (UIImagePickerController *)self.navigationController;

            // 不是相機才進行處理

            if (imagePickVC.sourceType != UIImagePickerControllerSourceTypeCamera) {

                [self _handleVCNavigationBarHidden:vc];

            }

        } else {

            // 不是相機才進行處理

            [self _handleVCNavigationBarHidden:vc];

        }

    });

 

- (void)_handleVCNavigationBarHidden:(UIViewController *)vc {

    [self.navigationController setNavigationBarHidden:vc.fd_prefersNavigationBarHidden animated:NO];

}

 

一、UIWebView無法左滑返回

原因:UIWebView默認是不開啟左滑手勢返回功能的,需要自己開啟;

解決:在viewDidLoad方法里面設置屬性fd_interactivePopDisabled為NO;

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.fd_interactivePopDisabled = NO;

}

 

二、UIWebView左滑返回沒有成功時或者取消返回時導航欄的title文字為空,不見了

原因:在viewWillDisappear:方法里面UIWebView控件被銷毀了;

ScanWebView = nil;

解決:

方法一: 最直接有效

在viewWillDisappear:方法里面取消UIWebView控件的銷毀;即把這行代碼注釋掉;ScanWebView = nil;//注釋掉

方法二: 比較復雜

在webViewDidFinishLoad:方法里面用一個變量保存獲取的title文字,在viewWillAppear:方法里面設置導航欄的title文字;

1、保存title文字的變量

@property (nonatomic, copy) NSString *titleString; //保存title文字的變量

2、設置導航欄的title文字

- (void)viewWillAppear:(BOOL)animated{ //設置導航欄的title文字

    [super viewWillAppear:animated];

    self.titleLabel.text = self.titleString;

}

 

3、用變量保存獲取到的title文字

- (void)webViewDidFinishLoad:(UIWebView *)webView{ //用變量保存獲取到的title文字

    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    self.titleString = title;

}

 

三、左滑返回沒有成功或者取消時,會出現 ... 的情況

原因:導航控制器的導航欄默認是有一個返回按鈕的;設置屬性hidesBackButton為YES就會出現這種情況;

解決:設置屬性hidesBackButton為NO,或者直接注釋掉這行代碼,不進行設置;

self.navigationItem.hidesBackButton = NO;

四、從無NavigationBar到有NavigationBar,手勢返回的時候,有NavigationBar的控制器導航欄會變成白色,或者消失不見了;

原因:在滑動的時候顯示出錯

解決:

在viewWillAppear:和viewWillDisappear:方法里面添加對是否隱藏NavigationBar的判斷;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    UIViewController *vc = [self.navigationController.viewControllers lastObject];

    if (vc.fd_prefersNavigationBarHidden) {

        [self.navigationController setNavigationBarHidden:YES animated:NO];

    } else {

        [self.navigationController setNavigationBarHidden:NO animated:NO];

    }

});

 

五、調用系統的發送短信控制器MFMessageComposeViewController時(如使用shareSDK進行短信分享時),右上角沒有“取消按鈕”,無法返回app

原因:好像被擋住了

解決:添加對控制器是否是MFMessageComposeViewController的判斷,是的話添加自己添加一個取消按鈕並添加方法的實現,

if ([self isKindOfClass:[MFMessageComposeViewController class]]) {

    [self fd_pushViewController:viewController animated:animated];

    [[self.viewControllers lastObject] navigationItem].rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissModal:)];

    return;

}

 

- (void)dismissModal:(UIButton *)sender{

    [[self.viewControllers lastObject] dismissViewControllerAnimated:YES completion:nil];

}

 

以上為使用過程遇到的坑以及解決辦法,很多也都是從GitHub搜集而來的,建議多到GitHub去看看;

附上FDFullscreenPopGesture的GitHub鏈接;https://github.com/forkingdog/FDFullscreenPopGesture

最后貼上在FDFullscreenPopGesture基礎上修改后的代碼:xx

 

// The MIT License (MIT)

//

// Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )

//

// Permission is hereby granted, free of charge, to any person obtaining a copy

// of this software and associated documentation files (the "Software"), to deal

// in the Software without restriction, including without limitation the rights

// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

// copies of the Software, and to permit persons to whom the Software is

// furnished to do so, subject to the following conditions:

//

// The above copyright notice and this permission notice shall be included in all

// copies or substantial portions of the Software.

//

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

// SOFTWARE.

 

#import "UINavigationController+FDFullscreenPopGesture.h"

#import <objc/runtime.h>

#import <MessageUI/MessageUI.h>

 

@interface _FDFullscreenPopGestureRecognizerDelegate : NSObject <UIGestureRecognizerDelegate>

 

@property (nonatomic, weak) UINavigationController *navigationController;

 

@end

 

@implementation _FDFullscreenPopGestureRecognizerDelegate

 

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer

{

    // Ignore when no view controller is pushed into the navigation stack.

    if (self.navigationController.viewControllers.count <= 1) {

        return NO;

    }

 

    // Ignore when the active view controller doesn't allow interactive pop.

    UIViewController *topViewController = self.navigationController.viewControllers.lastObject;

    if (topViewController.fd_interactivePopDisabled) {

        return NO;

    }

 

    // Ignore when the beginning location is beyond max allowed initial distance to left edge.

    CGPoint beginningLocation = [gestureRecognizer locationInView:gestureRecognizer.view];

    CGFloat maxAllowedInitialDistance = topViewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge;

    if (maxAllowedInitialDistance > 0 && beginningLocation.x > maxAllowedInitialDistance) {

        return NO;

    }

 

    // Ignore pan gesture when the navigation controller is currently in transition.

    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {

        return NO;

    }

 

    // Prevent calling the handler when the gesture begins in an opposite direction.

    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];

    if (translation.x <= 0) {

        return NO;

    }

 

    return YES;

}

 

@end

 

typedef void (^_FDViewControllerWillAppearInjectBlock)(UIViewController *viewController, BOOL animated);

 

@interface UIViewController (FDFullscreenPopGesturePrivate)

 

@property (nonatomic, copy) _FDViewControllerWillAppearInjectBlock fd_willAppearInjectBlock;

 

@end

 

@implementation UIViewController (FDFullscreenPopGesturePrivate)

 

+ (void)load

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        Class class = [self class];

 

        //viewWillAppear

        SEL originalSelector = @selector(viewWillAppear:);

        SEL swizzledSelector = @selector(fd_viewWillAppear:);

 

        Method originalMethod = class_getInstanceMethod(class, originalSelector);

        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

 

        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

        if (success) {

            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));

        } else {

            method_exchangeImplementations(originalMethod, swizzledMethod);

        }

 

        //viewWillDisappear

        SEL originalSelector2 = @selector(viewWillDisappear:);

        SEL swizzledSelector2 = @selector(fd_viewWillDisappear:);

 

        Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);

        Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);

 

        BOOL success2 = class_addMethod(class, originalSelector2, method_getImplementation(swizzledMethod2), method_getTypeEncoding(swizzledMethod2));

        if (success2) {

            class_replaceMethod(class, swizzledSelector2, method_getImplementation(originalMethod2), method_getTypeEncoding(originalMethod2));

        } else {

            method_exchangeImplementations(originalMethod2, swizzledMethod2);

        }

 

    });

}

 

- (void)fd_viewWillAppear:(BOOL)animated

{

    // Forward to primary implementation.

    [self fd_viewWillAppear:animated];

 

    if (self.fd_willAppearInjectBlock) {

        self.fd_willAppearInjectBlock(self, animated);

    }

 

    //設置導航的顯示/隱藏

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        UIViewController *vc = [self.navigationController.viewControllers lastObject];

        if (vc.fd_prefersNavigationBarHidden) {

            [self.navigationController setNavigationBarHidden:YES animated:NO];

        } else {

            [self.navigationController setNavigationBarHidden:NO animated:NO];

        }

    });

 

}

 

- (void)fd_viewWillDisappear:(BOOL)animated{

 

    [self fd_viewWillDisappear:animated];

 

    //設置導航的顯示/隱藏

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CGFLOAT_MIN * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        UIViewController *vc = [self.navigationController.viewControllers lastObject];

        if (vc.fd_prefersNavigationBarHidden) {

            [self.navigationController setNavigationBarHidden:YES animated:NO];

        } else {

            [self.navigationController setNavigationBarHidden:NO animated:NO];

        }

    });

 

}

 

- (_FDViewControllerWillAppearInjectBlock)fd_willAppearInjectBlock

{

    return objc_getAssociatedObject(self, _cmd);

}

 

- (void)setFd_willAppearInjectBlock:(_FDViewControllerWillAppearInjectBlock)block

{

    objc_setAssociatedObject(self, @selector(fd_willAppearInjectBlock), block, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

 

@end

 

@implementation UINavigationController (FDFullscreenPopGesture)

 

+ (void)load

{

    // Inject "-pushViewController:animated:"

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        Class class = [self class];

 

        SEL originalSelector = @selector(pushViewController:animated:);

        SEL swizzledSelector = @selector(fd_pushViewController:animated:);

 

        Method originalMethod = class_getInstanceMethod(class, originalSelector);

        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

 

        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

        if (success) {

            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));

        } else {

            method_exchangeImplementations(originalMethod, swizzledMethod);

        }

    });

}

 

- (void)fd_pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    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];

 

        // 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];

 

        // Disable the onboard gesture recognizer.

        self.interactivePopGestureRecognizer.enabled = NO;

    }

 

    // Handle perferred navigation bar appearance.

    [self fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:viewController];

 

    //過濾MessageUI,並且添加取消按鈕

    if ([self isKindOfClass:[MFMessageComposeViewController class]]) {

        [self fd_pushViewController:viewController animated:animated];

        [[self.viewControllers lastObject] navigationItem].rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissModal:)];

        return;

    }

 

    // Forward to primary implementation.

    if (![self.viewControllers containsObject:viewController]) {

        [self fd_pushViewController:viewController animated:animated];

    }

 

}

 

- (void)dismissModal:(UIButton *)sender

{

    [[self.viewControllers lastObject] dismissViewControllerAnimated:YES completion:nil];

}

 

- (void)fd_setupViewControllerBasedNavigationBarAppearanceIfNeeded:(UIViewController *)appearingViewController

{

    if (!self.fd_viewControllerBasedNavigationBarAppearanceEnabled) {

        return;

    }

 

    __weak typeof(self) weakSelf = self;

    _FDViewControllerWillAppearInjectBlock block = ^(UIViewController *viewController, BOOL animated) {

        __strong typeof(weakSelf) strongSelf = weakSelf;

        if (strongSelf) {

            [strongSelf setNavigationBarHidden:viewController.fd_prefersNavigationBarHidden animated:animated];

        }

    };

 

    // Setup will appear inject block to appearing view controller.

    // Setup disappearing view controller as well, because not every view controller is added into

    // stack by pushing, maybe by "-setViewControllers:".

    appearingViewController.fd_willAppearInjectBlock = block;

    UIViewController *disappearingViewController = self.viewControllers.lastObject;

    if (disappearingViewController && !disappearingViewController.fd_willAppearInjectBlock) {

        disappearingViewController.fd_willAppearInjectBlock = block;

    }

}

 

- (_FDFullscreenPopGestureRecognizerDelegate *)fd_popGestureRecognizerDelegate

{

    _FDFullscreenPopGestureRecognizerDelegate *delegate = objc_getAssociatedObject(self, _cmd);

 

    if (!delegate) {

        delegate = [[_FDFullscreenPopGestureRecognizerDelegate alloc] init];

        delegate.navigationController = self;

 

        objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    return delegate;

}

 

- (UIPanGestureRecognizer *)fd_fullscreenPopGestureRecognizer

{

    UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);

 

    if (!panGestureRecognizer) {

        panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];

        panGestureRecognizer.maximumNumberOfTouches = 1;

 

        objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    return panGestureRecognizer;

}

 

- (BOOL)fd_viewControllerBasedNavigationBarAppearanceEnabled

{

    NSNumber *number = objc_getAssociatedObject(self, _cmd);

    if (number) {

        return number.boolValue;

    }

    self.fd_viewControllerBasedNavigationBarAppearanceEnabled = YES;

    return YES;

}

 

- (void)setFd_viewControllerBasedNavigationBarAppearanceEnabled:(BOOL)enabled

{

    SEL key = @selector(fd_viewControllerBasedNavigationBarAppearanceEnabled);

    objc_setAssociatedObject(self, key, @(enabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

@end

 

@implementation UIViewController (FDFullscreenPopGesture)

 

- (BOOL)fd_interactivePopDisabled

{

    return [objc_getAssociatedObject(self, _cmd) boolValue];

}

 

- (void)setFd_interactivePopDisabled:(BOOL)disabled

{

    objc_setAssociatedObject(self, @selector(fd_interactivePopDisabled), @(disabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

- (BOOL)fd_prefersNavigationBarHidden

{

    return [objc_getAssociatedObject(self, _cmd) boolValue];

}

 

- (void)setFd_prefersNavigationBarHidden:(BOOL)hidden

{

    objc_setAssociatedObject(self, @selector(fd_prefersNavigationBarHidden), @(hidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

 

- (CGFloat)fd_interactivePopMaxAllowedInitialDistanceToLeftEdge

{

#if CGFLOAT_IS_DOUBLE

    return [objc_getAssociatedObject(self, _cmd) doubleValue];

#else

    return [objc_getAssociatedObject(self, _cmd) floatValue];

#endif

}

 

- (void)setFd_interactivePopMaxAllowedInitialDistanceToLeftEdge:(CGFloat)distance

{

    SEL key = @selector(fd_interactivePopMaxAllowedInitialDistanceToLeftEdge);

    objc_setAssociatedObject(self, key, @(MAX(0, distance)), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

@end

最后貼上改善后的GitHub地址  https://github.com/1570383140/FDFullscreenPopGesture.git

作者:zhhelnice 

來源:CSDN 

原文:https://blog.csdn.net/zhhelnice/article/details/60580637 

 

作者:oceanfive

鏈接:https://www.jianshu.com/p/bbc2305d83e2

來源:簡書

簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM