需要注意的是手势代理对象的保存于转交问题!!!
UIGestureRecognizerDelegate
自定义无法实现手势效果图 Gif
自定义实现手势效果图 Gif
ViewController.m
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark - lifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)goToNextVC:(id)sender {
NextViewController *nextVC = [[NextViewController alloc] init];
[self.navigationController pushViewController:nextVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
NextViewController.m
#import "NextViewController.h"
@interface NextViewController () <UIGestureRecognizerDelegate>
//变量用来保存代理:
@property (nonatomic, strong) id<UIGestureRecognizerDelegate> originDelegate;
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
//自定义UIBarButtonItem 虽然满足了 UI 的效果,但是却导致了左侧边缘滑动失效,得不偿失!
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imgBack"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
self.navigationItem.leftBarButtonItem = leftButtonItem;
//侧滑返回交互手势:UIGestureRecognizer *interactivePopGestureRecognizer
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
//返回上一页:
- (void)leftBarButtonItemAction {
[self.navigationController popViewControllerAnimated:YES];
}
//视图已经出现的时候,让成员变量 originDelegate 保存 导航控制器的交互返回手势识别的代理:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.originDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
//当视图将要消失的时候再把刚刚保存的导航控制器的交互返回手势识别代理变量重新置为系统自身的!
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.interactivePopGestureRecognizer.delegate = self.originDelegate;
self.originDelegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end