iOS右滑返回


iOS系統支持邊緣右滑返回,但這樣的前提是用的是系統的navigationItem的backBarButtonItem。

但是很多情況下我們的navigationItem都是自定義的,這樣就回失去右滑返回的效果。建議大家如果有需要自定義的nav,最好整個app內統一都用自定義的nav bar來寫,不然添加右滑返回後,可能會出現導航條錯亂的問題


方法一:在baseController的viewDidLoad裏寫如下代碼

              self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

              但是我試了這樣的方法,可能引起程序卡死甚至崩潰。查了網上很多方法,都試了但是也沒有解決我的問題,所以我沒有采用這個方法


方法二:在baseControllerviewDidLoad裏添加

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(back:)];

               swipe.delegate = self;

              [self.view addGestureRecognizer:swipe];

              self.navigationController.interactivePopGestureRecognizer.enabled = NO;

           然後實現back方法

             - (void)back:(UISwipeGestureRecognizer*)swipe {

                     if (self.navigationController.viewControllers.count <= 1) return;

                     [self.navigationController popViewControllerAnimated:YES];

              }

          方法二的弊端也很明顯,不能看到右滑的中間態,只要觸發右滑手勢就回返回到上一級目錄


方法三:在baseControllerviewDidLoad添加

               id target = self.navigationController.interactivePopGestureRecognizer.delegate;

              UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target            action:@selector(handleNavigationTransition:)];

              pan.delegate = self;

              [self.view addGestureRecognizer:pan];   

              self.navigationController.interactivePopGestureRecognizer.enabled = NO;

這樣後發現不光是向右滑動,連向左滑動也會返回,不要着急,再加我下邊的這個方法就搞定啦。

            

          - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {

                 CGPoint point = [gestureRecognizer translationInView:self.view];

                 if (point.x > 0) {

                        return YES;

                 } else {

                       return NO;

                 }

          }

搞定了,可以試試哦,有什麼問題可以直接留言,我們一起討論哦


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章