蘋果一直都在人機互動中儘力做到極致,在iOS7中,新增加了一個小小的功能,也就是這個api:self.navigationController.interactivePopGestureRecognizer.enabled = YES;
這個api功能就是在NavigationController堆棧內的UIViewController可以支援右滑手勢,也就是不用點擊右上方的返回按鈕,輕輕在螢幕左邊一滑,螢幕就會返回,隨著ios裝置螢幕的增大,這個小功能讓手指短,拇指大和手殘人士看到了福音。
這個功能是好,但是經常我們會有需求定製返回按鈕,如果手動定製了返回按鈕,這個功能將會失效,也就是自訂了navigationItem的leftBarButtonItem,那麼這個手勢就會失效。
解決方案找到兩種
1.重新設定手勢的delegate
複製代碼 代碼如下:
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
2.當然你也可以自己響應這個手勢的事件
複製代碼 代碼如下:
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
有更多方法以後繼續補充,這裡可以根據自己需要進行選擇,如果只是簡單定製了返回按鈕,第一種最簡單,一句代碼搞定。
問題二:ios開發 向右滑動手勢實現返回.在NavigationController中如何設定
在navigationController中實現向右滑動 返回功能
系統提供的backbarbuttonitem,不用添加任何代碼即可實現向右滑動後退功能,但是往往要對按鈕修改樣式等時,就需要自訂leftbarbuttonitem,此時向右滑動即失效.通過下面方法即可解決該問題.(本人親自實驗過)
主要是通過設定navigationController.interactivePopGestureRecognizer 此手勢的一些屬性,此手勢大家可以通過sdk查看說明,這裡不細說
1. self.navigationController.interactivePopGestureRecognizer.enabled = YES | NO; 手勢有效與否
2. self.navigationController.interactivePopGestureRecognizer.delegate = self; 手勢的代理,一般會設定為self
1中的屬性,再viewcontroller中預設的設定為YES,即手勢有效.按照2中的屬性設定後,當前的viewcontroller即可以實現該向右滑動後退功能,但是當回到navigationController的rootView中再次做出向右滑動時,程式會有問題(再次push子controller時,程式卡在當前介面無法跳轉).有效解決方案如下:
說明:有兩個controllerA,B
navigationController的rootview設定為A,在A中點擊按鈕後push B.在A的 -(void)viewDidAppear:(BOOL)animated方法中加入self.navigationController.interactivePopGestureRecognizer.enabled = NO;代碼如下:- (void)viewDidAppear:(BOOL)animated{ if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; //讓rootView禁止滑動 }}
然後再B中的- (void)viewDidLoad方法中加入
- (void)viewDidLoad{ // 配置返回按鈕 UIBarButtonItem * backItem = [self barButtonForImageNames:@[@"icon-返回", @"", @""] action:@selector(popBack)]; backItem.title = @""; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = self; } self.navigationItem.leftBarButtonItem = backItem; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { self.navigationController.interactivePopGestureRecognizer.delegate = self; }}
這樣即可以保證再A中向右滑動後再次pushB時不會卡在A介面.
再項目中大家一般會建立風格統一的介面,一般都會建立一個基礎viewcontroller,再此viewcontroller擴充一個配置leftbarbutton的方法,在該方法中加入B的viewDidLoad中的代碼,這樣在建立leftbarbutton的同時,直接加了返回的操作(不用每個viewController中都加入這樣一段代碼).然後只在navigationController的rootView中加入A的(void)viewDidAppear:(BOOL)animated方法即可.
以上內容是Android NavigationController 右滑手勢詳解,希望大家喜歡。