Swift - UITapGestureRecognizer UIPageViewController
Posted
技术标签:
【中文标题】Swift - UITapGestureRecognizer UIPageViewController【英文标题】: 【发布时间】:2015-04-21 23:50:39 【问题描述】:好的,我已经创建了一个浏览量滑块应用。一切都很好,但现在我希望能够使用 UITapGestureRecognizer 通过单击来隐藏/取消隐藏我的导航栏。
我已经能够在 viewDidLoad 上隐藏导航栏,但我不确定在哪里调用我的 singleTapped 函数,或者如何实现 UITapGestureRecognizer。
如何使用 UITapGestureRecognizer 隐藏/取消隐藏 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
【问题讨论】:
【参考方案1】:使用 UITapGestureRecognizer:
在viewDidLoad函数中添加
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"singleTapped:"))
【讨论】:
以上是关于Swift - UITapGestureRecognizer UIPageViewController的主要内容,如果未能解决你的问题,请参考以下文章
swift [Swift Notes]在学习Swift #Swift的同时收集笔记