Swift導航控制器(navigationController)全屏滑動返回功能實現

來源:互聯網
上載者:User

navigationController(導航控制器)的view內建了滑動手勢,只要在螢幕左側向右拖動頁面,就可以滑動返回到前面一個頁面。但這個功能僅在螢幕左側邊緣滑動才能觸發,我們可以稍作改造,讓其支援全屏滑動返回。


1,全屏滑動返回實現原理

(1)系統內建的手勢是 UIScreenEdgePanGestureRecognizer 類型對象,看名字就知道這個是螢幕邊緣滑動手勢。所以系統內建的滑動效果,自然只能實現側邊滑動。
(2)我們自己給導航控制器,添加一個全屏的滑動手勢。然後用新添加的滑動手勢,來調用系統實現的滑動返回功能,這樣就實現了全屏滑動功能。

(3)注意:我們還要禁止系統內建滑動手勢,同時只有非根控制器才有滑動返回功能,根控制器沒有。

2,效果圖:


3,實現代碼:

import UIKit
 
class DetailViewController: UIViewController,  UIGestureRecognizerDelegate {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let target = self.navigationController?.interactivePopGestureRecognizer!.delegate
        let pan = UIPanGestureRecognizer(target:target,
            action:Selector("handleNavigationTransition:"))
        pan.delegate = self
        self.view.addGestureRecognizer(pan)
        self.navigationController?.interactivePopGestureRecognizer!.enabled = false
    }
    
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer,
        shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer:
        UIGestureRecognizer) -> Bool {
        if self.childViewControllers.count == 1 {
            return false
        }
        return true
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

原文出自:www.hangge.com

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.