From 0aae95c61df33e4d975b9416147398856b5d4ece Mon Sep 17 00:00:00 2001 From: asaake Date: Fri, 3 Mar 2017 18:04:30 +0900 Subject: [PATCH] Added trackingPanGestureRecognizer to use interactivePopGestureRecognizer.delegate --- .../HidingNavigationBarManager.swift | 7 +- .../project.pbxproj | 12 +++ .../HiddenNavViewController.swift | 82 ++++++++++++++++++ .../HidingNavShowsNavViewController.swift | 85 +++++++++++++++++++ ...eractivePopGestureRecognizerDelegate.swift | 13 +++ .../MasterViewController.swift | 11 ++- 6 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 HidingNavigationBarSample/HidingNavigationBarSample/HiddenNavViewController.swift create mode 100644 HidingNavigationBarSample/HidingNavigationBarSample/HidingNavShowsNavViewController.swift create mode 100644 HidingNavigationBarSample/HidingNavigationBarSample/InteractivePopGestureRecognizerDelegate.swift diff --git a/HidingNavigationBar/HidingNavigationBarManager.swift b/HidingNavigationBar/HidingNavigationBarManager.swift index 2021fbe..62a49de 100644 --- a/HidingNavigationBar/HidingNavigationBarManager.swift +++ b/HidingNavigationBar/HidingNavigationBarManager.swift @@ -58,7 +58,9 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture // Hiding navigation bar state fileprivate var currentState = HidingNavigationBarState.Open fileprivate var previousState = HidingNavigationBarState.Open - + + fileprivate var trackingScrollViewPanGestureRecognizer: UIPanGestureRecognizer! + //Options open var onForegroundAction = HidingNavigationForegroundAction.default @@ -87,6 +89,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture let panGesture = UIPanGestureRecognizer(target: self, action: #selector(HidingNavigationBarManager.handlePanGesture(_:))) panGesture.delegate = self scrollView.addGestureRecognizer(panGesture) + trackingScrollViewPanGestureRecognizer = panGesture navBarController.expandedCenter = {[weak self] (view: UIView) -> CGPoint in return CGPoint(x: view.bounds.midX, y: view.bounds.midY + (self?.statusBarHeight() ?? 0)) @@ -138,6 +141,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture } open func viewWillAppear(_ animated: Bool) { + trackingScrollViewPanGestureRecognizer.isEnabled = true expand() } @@ -147,6 +151,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture open func viewWillDisappear(_ animated: Bool) { expand() + trackingScrollViewPanGestureRecognizer.isEnabled = false } open func updateValues() { diff --git a/HidingNavigationBarSample/HidingNavigationBarSample.xcodeproj/project.pbxproj b/HidingNavigationBarSample/HidingNavigationBarSample.xcodeproj/project.pbxproj index 9402968..7c74ede 100644 --- a/HidingNavigationBarSample/HidingNavigationBarSample.xcodeproj/project.pbxproj +++ b/HidingNavigationBarSample/HidingNavigationBarSample.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 52258B381E6412EA007E9A19 /* HiddenNavViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52258B371E6412EA007E9A19 /* HiddenNavViewController.swift */; }; + 52258B3A1E6413F2007E9A19 /* HidingNavShowsNavViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52258B391E6413F2007E9A19 /* HidingNavShowsNavViewController.swift */; }; + 52258B431E66E55A007E9A19 /* InteractivePopGestureRecognizerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52258B421E66E55A007E9A19 /* InteractivePopGestureRecognizerDelegate.swift */; }; 6A412A2E1BB1CBA4001C3F67 /* HidingNavigationBar.h in Headers */ = {isa = PBXBuildFile; fileRef = 6A412A2D1BB1CBA4001C3F67 /* HidingNavigationBar.h */; settings = {ATTRIBUTES = (Public, ); }; }; 6A412A401BB1CBA4001C3F67 /* HidingNavigationBar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A412A2B1BB1CBA4001C3F67 /* HidingNavigationBar.framework */; }; 6A412A411BB1CBA4001C3F67 /* HidingNavigationBar.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 6A412A2B1BB1CBA4001C3F67 /* HidingNavigationBar.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -58,6 +61,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 52258B371E6412EA007E9A19 /* HiddenNavViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HiddenNavViewController.swift; sourceTree = ""; }; + 52258B391E6413F2007E9A19 /* HidingNavShowsNavViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HidingNavShowsNavViewController.swift; sourceTree = ""; }; + 52258B421E66E55A007E9A19 /* InteractivePopGestureRecognizerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InteractivePopGestureRecognizerDelegate.swift; sourceTree = ""; }; 6A412A2B1BB1CBA4001C3F67 /* HidingNavigationBar.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HidingNavigationBar.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 6A412A2D1BB1CBA4001C3F67 /* HidingNavigationBar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HidingNavigationBar.h; sourceTree = ""; }; 6A412A2F1BB1CBA4001C3F67 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -156,6 +162,9 @@ 6A65711D1AF3FDB600A6CFE4 /* HidingNavExtensionViewController.swift */, 6A6571231AF40E7000A6CFE4 /* HidingNavTabViewController.swift */, 6A65711F1AF408C000A6CFE4 /* HidingNavToolbarViewController.swift */, + 52258B371E6412EA007E9A19 /* HiddenNavViewController.swift */, + 52258B391E6413F2007E9A19 /* HidingNavShowsNavViewController.swift */, + 52258B421E66E55A007E9A19 /* InteractivePopGestureRecognizerDelegate.swift */, 6A6570F31AF3F71100A6CFE4 /* Main.storyboard */, 6A6570F61AF3F71100A6CFE4 /* Images.xcassets */, 6A6570F81AF3F71100A6CFE4 /* LaunchScreen.xib */, @@ -355,11 +364,14 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 52258B381E6412EA007E9A19 /* HiddenNavViewController.swift in Sources */, 6A6571211AF408C000A6CFE4 /* HidingNavToolbarViewController.swift in Sources */, 6A65711B1AF3FADC00A6CFE4 /* HidingNavigationBarManager.swift in Sources */, 6A6571241AF40E7000A6CFE4 /* HidingNavTabViewController.swift in Sources */, 6A6570F01AF3F71100A6CFE4 /* MasterViewController.swift in Sources */, 6A6570EE1AF3F71100A6CFE4 /* AppDelegate.swift in Sources */, + 52258B3A1E6413F2007E9A19 /* HidingNavShowsNavViewController.swift in Sources */, + 52258B431E66E55A007E9A19 /* InteractivePopGestureRecognizerDelegate.swift in Sources */, 6A6571121AF3F8D900A6CFE4 /* HidingNavViewController.swift in Sources */, 6A65711C1AF3FADC00A6CFE4 /* HidingViewController.swift in Sources */, 6A65711E1AF3FDB600A6CFE4 /* HidingNavExtensionViewController.swift in Sources */, diff --git a/HidingNavigationBarSample/HidingNavigationBarSample/HiddenNavViewController.swift b/HidingNavigationBarSample/HidingNavigationBarSample/HiddenNavViewController.swift new file mode 100644 index 0000000..c32261b --- /dev/null +++ b/HidingNavigationBarSample/HidingNavigationBarSample/HiddenNavViewController.swift @@ -0,0 +1,82 @@ +// +// HiddenNavViewController.swift +// HidingNavigationBarSample +// +// Created by asaake on 2017/02/27. +// Copyright (c) 2017 Tristan Himmelman. All rights reserved. +// + +import UIKit + +class HiddenNavViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { + + let identifier = "cell" + var tableView: UITableView! + + override func viewDidLoad() { + super.viewDidLoad() + + tableView = UITableView(frame: view.bounds) + tableView.dataSource = self + tableView.delegate = self + tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: identifier) + view.addSubview(tableView) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + self.navigationController?.setNavigationBarHidden(true, animated: animated) + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + } + + // MARK: UITableViewDelegate + + func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { + return true + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if indexPath.row == 0 { + dismiss(animated: true, completion: nil) + } else { + let controller = HidingNavShowsNavViewController() + navigationController?.pushViewController(controller, animated: true) + } + } + + // MARK: - Table view data source + + func numberOfSections(in tableView: UITableView) -> Int { + // #warning Potentially incomplete method implementation. + // Return the number of sections. + return 1 + } + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + // #warning Incomplete method implementation. + // Return the number of rows in the section. + return 100 + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) + + // Configure the cell... + if indexPath.row == 0 { + cell.textLabel?.text = "close controller"; + cell.selectionStyle = UITableViewCellSelectionStyle.none + } else { + cell.textLabel?.text = "row \((indexPath as NSIndexPath).row)" + cell.selectionStyle = UITableViewCellSelectionStyle.none + } + return cell + } + +} diff --git a/HidingNavigationBarSample/HidingNavigationBarSample/HidingNavShowsNavViewController.swift b/HidingNavigationBarSample/HidingNavigationBarSample/HidingNavShowsNavViewController.swift new file mode 100644 index 0000000..98bffab --- /dev/null +++ b/HidingNavigationBarSample/HidingNavigationBarSample/HidingNavShowsNavViewController.swift @@ -0,0 +1,85 @@ +// +// HidingNavShowsNavViewController.swift +// HidingNavigationBarSample +// +// Created by asaake on 2017/02/27. +// Copyright (c) 2017 Tristan Himmelman. All rights reserved. +// + +import UIKit + +class HidingNavShowsNavViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { + + let identifier = "cell" + var hidingNavBarManager: HidingNavigationBarManager? + var tableView: UITableView! + + override func viewDidLoad() { + super.viewDidLoad() + + tableView = UITableView(frame: view.bounds) + tableView.dataSource = self + tableView.delegate = self + tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: identifier) + view.addSubview(tableView) + + let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(HidingNavShowsNavViewController.backButtonTouched)) + navigationItem.leftBarButtonItem = backButton + + hidingNavBarManager = HidingNavigationBarManager(viewController: self, scrollView: tableView) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + navigationController?.setNavigationBarHidden(false, animated: animated) + tableView.panGestureRecognizer.require(toFail: navigationController!.interactivePopGestureRecognizer!) + hidingNavBarManager?.viewWillAppear(animated) + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + hidingNavBarManager?.viewDidLayoutSubviews() + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + hidingNavBarManager?.viewWillDisappear(animated) + } + + func backButtonTouched(){ + _ = navigationController?.popViewController(animated: true) + } + + // MARK: UITableViewDelegate + + func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { + hidingNavBarManager?.shouldScrollToTop() + + return true + } + + // MARK: - Table view data source + + func numberOfSections(in tableView: UITableView) -> Int { + // #warning Potentially incomplete method implementation. + // Return the number of sections. + return 1 + } + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + // #warning Incomplete method implementation. + // Return the number of rows in the section. + return 100 + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) + + // Configure the cell... + cell.textLabel?.text = "row \((indexPath as NSIndexPath).row)" + cell.selectionStyle = UITableViewCellSelectionStyle.none + + return cell + } + +} diff --git a/HidingNavigationBarSample/HidingNavigationBarSample/InteractivePopGestureRecognizerDelegate.swift b/HidingNavigationBarSample/HidingNavigationBarSample/InteractivePopGestureRecognizerDelegate.swift new file mode 100644 index 0000000..1955c75 --- /dev/null +++ b/HidingNavigationBarSample/HidingNavigationBarSample/InteractivePopGestureRecognizerDelegate.swift @@ -0,0 +1,13 @@ +// +// InteractivePopGestureRecognizerDelegate.swift +// HidingNavigationBarSample +// +// Created by asaake on 2017/03/01. +// Copyright (c) 2017 Tristan Himmelman. All rights reserved. +// + +import UIKit + +class InteractivePopGestureRecognizerDelegate: NSObject, UIGestureRecognizerDelegate { + +} diff --git a/HidingNavigationBarSample/HidingNavigationBarSample/MasterViewController.swift b/HidingNavigationBarSample/HidingNavigationBarSample/MasterViewController.swift index 816b256..9e43a0c 100644 --- a/HidingNavigationBarSample/HidingNavigationBarSample/MasterViewController.swift +++ b/HidingNavigationBarSample/HidingNavigationBarSample/MasterViewController.swift @@ -10,7 +10,7 @@ import UIKit class MasterViewController: UITableViewController { - let rows = ["Hiding Nav Bar", "Hiding Nav Bar + Extension View", "Hiding Nav Bar + Toolbar", "Hiding Nav Bar + TabBar"] + let rows = ["Hiding Nav Bar", "Hiding Nav Bar + Extension View", "Hiding Nav Bar + Toolbar", "Hiding Nav Bar + TabBar", "Hidden Shows NavBar + InteractivePopGestureRecognizer"] override func viewDidLoad() { super.viewDidLoad() @@ -65,7 +65,7 @@ class MasterViewController: UITableViewController { } else if (indexPath as NSIndexPath).row == 2 { let controller = HidingNavToolbarViewController() navigationController?.pushViewController(controller, animated: true) - } else { + } else if (indexPath as NSIndexPath).row == 3 { let controller1 = HidingNavTabViewController() let navController1 = UINavigationController(rootViewController: controller1) navController1.tabBarItem = UITabBarItem(tabBarSystemItem: .mostRecent, tag: 0) @@ -78,8 +78,13 @@ class MasterViewController: UITableViewController { let tabBarController = UITabBarController() tabBarController.viewControllers = [navController1, navController2] navigationController?.present(tabBarController, animated: true, completion: nil) + } else { + let controller = HiddenNavViewController() + let navController = UINavigationController(rootViewController: controller) + navController.interactivePopGestureRecognizer?.delegate = InteractivePopGestureRecognizerDelegate() + styleNavigationController(navController) + navigationController?.present(navController, animated: true, completion: nil) } - } }