iOS侧滑返回

2018-04-26  本文已影响59人  钦川

相关原理

iOS侧滑返回,有三种方案可以实现(只考虑iOS7以后)

我的实践

1.QqcNavigationController继承自UINavigationController,替换系统侧滑手势的委托对象
QqcNavigationController.h

#import <UIKit/UIKit.h>

@interface QqcNavigationController : UINavigationController

//是否开启滑动退场
@property(nonatomic, assign)BOOL isPanBackGestureEnable;

@end

QqcNavigationController.m

#import "QqcNavigationController.h"

@interface QqcNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation QqcNavigationController

#pragma mark - system framwork
- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
}

#pragma mark - property
- (void)setIsPanBackGestureEnable:(BOOL)isPanBackGestureEnable {
    _isPanBackGestureEnable = isPanBackGestureEnable;
    self.interactivePopGestureRecognizer.enabled = _isPanBackGestureEnable;
}

#pragma mark - UIGestureRecognizerDelegate
// 手势的代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    BOOL isShould = NO;
    if ( (self.childViewControllers.count<=1) ) {
        isShould = NO;
    }else {
        isShould = YES;
    }
    return isShould;
}

@end

2.在BaseViewController中当viewDidDisappear时禁用,viewDidAppear时根据情况禁用或开启
BaseViewController.h

#import "QqcBaseViewController.h"

@interface BaseViewController : QqcBaseViewController

#pragma mark - polymorphy
//是否需要滑动退场
- (BOOL)isPanBackGestureEnable;

@end

BaseViewController.m

#import "BaseViewController.h"
#import "QqcNavigationController.h"

@implementation BaseViewController

#pragma mark - polymorphy
//是否需要全屏滑动退场
- (BOOL)isPanBackGestureEnable {
    return YES;
}

#pragma mark - system framework
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //是否启用全屏滑动手势
    BOOL isPanBackEnable = [self isPanBackGestureEnable];
    ((QqcNavigationController*)self.navigationController).isPanBackGestureEnable = isPanBackEnable;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //启用全屏滑动手势
    ((QqcNavigationController*)self.navigationController).isPanBackGestureEnable = NO;
}

@end

3.在SomeoneViewController中定制是否需要滑动返回手势

//是否需要滑动退场
- (BOOL)isPanBackGestureEnable {
    return NO;
}
上一篇 下一篇

猜你喜欢

热点阅读