方式一:
.h
#import <UIKit/UIKit.h>
@interface MTMainNavigationC : UINavigationController
@end
.m
//
// MTMainNavigationC.m
// MTDemo_date
//
// Created by apple on 15/12/30.
// Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//
#import "MTMainNavigationC.h"
@interface MTMainNavigationC ()<UINavigationControllerDelegate>
@end
@implementation MTMainNavigationC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
}
// 导航控制器加载完毕一个View的时候就会调用这个方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController == self.childViewControllers[0])
{
[self addGesture:nil];
}else{
[self addGesture:@selector(handleNavigationTransition:)];
}
}
- (void)addGesture:(SEL)action
{
UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;
gesture.enabled = NO;
NSMutableArray *targets = [gesture valueForKeyPath:@"_targets"];
id gestureTarget = [targets firstObject];
id target = [gestureTarget valueForKeyPath:@"_target"];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
[gesture.view addGestureRecognizer:pan];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
方式二:
.h
//
// MTMainNavigationTwoC.h
// MTDemo_date
//
// Created by apple on 15/12/30.
// Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MTMainNavigationTwoC : UINavigationController
@end
.m
//
// MTMainNavigationTwoC.m
// MTDemo_date
//
// Created by apple on 15/12/30.
// Copyright © 2015年 Bocheng network technology co., LTD. All rights reserved.
//
#import "MTMainNavigationTwoC.h"
@interface MTMainNavigationTwoC ()<UIGestureRecognizerDelegate>
@end
@implementation MTMainNavigationTwoC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 获取屏幕边缘手势识别器
UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer *)self.interactivePopGestureRecognizer;
// 先禁止系统的左侧滑手势
gesture.enabled = NO;
// 利用KVC强制获取手势数组
// 利用运行时查看私有属性
NSMutableArray *targets = [gesture valueForKeyPath:@"_targets"];
// 获取这个这个手势对象
id gestureTarget = [targets firstObject];
// 利用KVC获取手势的私有属性target
id target = [gestureTarget valueForKeyPath:@"_target"];
// 创建滑动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// 设置自定义的手势代理为控制器,这样自定义的手势就会调用代理的这个方法`gestureRecognizerShouldBegin:`
pan.delegate = self;
// 添加自定义手势到手势识别器的View中
[gesture.view addGestureRecognizer:pan];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// 这里有两个条件不允许手势执行:1、当前控制器为根控制器;2、如果这个push、pop动画正在执行(私有属性)
return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end