為了給導(dǎo)航頁(yè)面添加手勢(shì)拖動(dòng)操作女气,需要重寫UINavigationController。
1.建立一個(gè)名字叫JOPNavigationController的UINavigationController的子類掉缺。
JOPNavigationController.h
<pre>12312312312312312312312312312312312312312312312312312312312312312312312312312312312312313</pre>
2.在.m文件的viewDidLoad方法中添加如下代碼
/ 獲取系統(tǒng)自帶滑動(dòng)手勢(shì)的target對(duì)象? ?
id target = self.interactivePopGestureRecognizer.delegate;
?// 創(chuàng)建全屏滑動(dòng)手勢(shì),調(diào)用系統(tǒng)自帶滑動(dòng)手勢(shì)的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
? ? // 設(shè)置手勢(shì)代理局骤,攔截手勢(shì)觸發(fā)
pan.delegate=self;
? // 給導(dǎo)航控制器的view添加全屏滑動(dòng)手勢(shì)
? ?[self.view addGestureRecognizer:pan];
? ? // 禁止使用系統(tǒng)自帶的滑動(dòng)手勢(shì)
? ? self.interactivePopGestureRecognizer.enabled = NO;
3.重寫判斷手勢(shì)觸發(fā)的方法
//下面兩個(gè)方法只需返回YES,就可以攀圈,可以添加限制條件
// 攔截手勢(shì)觸發(fā)
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer*)gestureRecognizer
{
? ? return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch{
? ? return YES;
}
4.新建的子類就可以替代系統(tǒng)的UINavigationController來(lái)用了暴凑。
具體用法如下所示:
在AppDelegate的didFinishLaunchingWithOptions方法中添加如下代碼:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];?
ViewController *rootViewController = [[ViewController alloc] init];? ?
?JOPNavigationController *navigationController = [[JOPNavigationController alloc] initWithRootViewController:rootViewController];?
[self.window setRootViewController:navigationController];
該方法的實(shí)現(xiàn)原理以后有時(shí)間再詳細(xì)討論峦甩。