Swift - UITapGestureRecognizer UIPageViewController

Swift - UITapGestureRecognizer UIPageViewController

好的,我已经创建了一个综合浏览量滑块应用程序。一切都很好,但现在我希望能够使用 UITapGestureRecognizer hide/unhide 我的导航栏只需点击一下。

我已经能够在 viewDidLoad 上隐藏导航栏,但我不确定在哪里调用我的 singleTapped 函数,或者如何实现 UITapGestureRecognizer。

如何使用 UITapGestureRecognizer hide/unhide UIPageViewController 导航栏?

新 swift/ios

我的代码

class ViewController: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!

private var allPages = [Page]()
var pages = NSMutableOrderedSet()

override func viewDidLoad() {
    super.viewDidLoad()

    ###Hide NavBar
    self.navigationController?.navigationBarHidden = true

    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self

    var startVC = self.viewControllerAtIndex(0) as ContentViewController
    var viewControllers = NSArray(object: startVC)
    self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}

###Function to Hide/Unhide Navbar
func singleTapped(recognizer: UITapGestureRecognizer) {

    if (self.navigationController?.navigationBarHidden == false) {
        ###hide the Navigation Bar
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
        ###if Navigation Bar is already hidden
    else if (self.navigationController?.navigationBarHidden == true)
    {
        ###Show the Navigation Bar
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == 0 || index == NSNotFound) {
        return nil
    }

    index--
    return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == NSNotFound) {
        return nil
    }

    index++

    if (index == self.pages.count) {
        return nil
    }

    return self.viewControllerAtIndex(index)

}

func viewControllerAtIndex(index: Int) -> ContentViewController
{
    if ((self.pages.count == 0) || (index >= self.pages.count)) {
        return ContentViewController()
    }

    var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

    ###Store image in imageFile
    var image = (pages.objectAtIndex(index) as! Page).image        
    vc.imageFile = image
    vc.pageIndex = index

    return vc

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return self.pages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return 0
}

} 

使用 UITapGestureRecognizer:

在viewDidLoad函数中添加

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"singleTapped:"))