From 5ae77f63ab6af9d4bedcbdcd67983260c1ff9e7f Mon Sep 17 00:00:00 2001 From: wenlei Date: Fri, 2 Feb 2024 10:42:02 +0800 Subject: [PATCH] Handle navigation bar click issues --- .../IndieMusic/Common/NavigationBar.swift | 24 ++++-------- .../Common/NavigationController.swift | 4 ++ .../IndieMusic/Modules/Home/HomeView.swift | 2 + .../Modules/Home/HomeViewController.swift | 37 ++++++++----------- 4 files changed, 28 insertions(+), 39 deletions(-) diff --git a/IndieMusic/IndieMusic/Common/NavigationBar.swift b/IndieMusic/IndieMusic/Common/NavigationBar.swift index f37eac3..03db4f9 100644 --- a/IndieMusic/IndieMusic/Common/NavigationBar.swift +++ b/IndieMusic/IndieMusic/Common/NavigationBar.swift @@ -7,24 +7,15 @@ import Foundation -protocol NavigationBarTouchDelegate: AnyObject { - func navigationBar(_ navigationBar: NavigationBar, hitTest point: CGPoint, with event: UIEvent?) -} - class NavigationBar: UINavigationBar { - weak var touchDelegate: NavigationBarTouchDelegate? - private var isHitTestHandled = false + weak var passthroughView: UIView? + override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { - - let playButtonPoint = convert(point, to: self) - if self.point(inside: playButtonPoint, with: event) && !isHitTestHandled { - isHitTestHandled = true - touchDelegate?.navigationBar(self, hitTest: point, with: event) - DispatchQueue.main.async { - self.isHitTestHandled = false - } - return self + let navPoint = convert(point, to: self) + + if let passthroughView = passthroughView, self.point(inside: navPoint, with: event) { + return passthroughView.hitTest(convert(point, to: passthroughView), with: event) } return super.hitTest(point, with: event) } @@ -32,12 +23,11 @@ class NavigationBar: UINavigationBar { override init(frame: CGRect) { super.init(frame: frame) - + } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - } diff --git a/IndieMusic/IndieMusic/Common/NavigationController.swift b/IndieMusic/IndieMusic/Common/NavigationController.swift index d9a6cd5..9fec556 100644 --- a/IndieMusic/IndieMusic/Common/NavigationController.swift +++ b/IndieMusic/IndieMusic/Common/NavigationController.swift @@ -63,6 +63,10 @@ class NavigationController: UINavigationController { appearance.shadowColor = .clear +// if let navigationBar = navigationBar as? NavigationBar { +// navigationItem.titleView = navigationBar.titleView +// } + } diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeView.swift b/IndieMusic/IndieMusic/Modules/Home/HomeView.swift index 49ead5b..6f77cc8 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeView.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeView.swift @@ -620,3 +620,5 @@ class HomePagerView: UIView { } + + diff --git a/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift b/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift index 0a97ab8..0fcbbed 100644 --- a/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift +++ b/IndieMusic/IndieMusic/Modules/Home/HomeViewController.swift @@ -14,9 +14,6 @@ import Kingfisher import ETNavBarTransparent class HomeViewController: TableViewController { - - - var homePagerView: HomePagerView = { let homePagerView = HomePagerView.init() @@ -33,10 +30,7 @@ class HomeViewController: TableViewController { navigationItem.title = "期刊推荐" - - if let navigationBar = self.navigationController?.navigationBar as? NavigationBar { - navigationBar.touchDelegate = self - } + } @@ -52,6 +46,18 @@ class HomeViewController: TableViewController { tabbar.showAllBar(true, animated: true) } + if let navigationBar = self.navigationController?.navigationBar as? NavigationBar { + navigationBar.passthroughView = homePagerView + } + + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + if let navigationBar = self.navigationController?.navigationBar as? NavigationBar { + navigationBar.passthroughView = nil + } } override func makeUI() { @@ -101,8 +107,6 @@ class HomeViewController: TableViewController { self?.navigator.show(segue: .journalDetail(viewModel: journalDetailViewModel), sender: self) - default: break - } }.disposed(by: rx.disposeBag) @@ -160,6 +164,7 @@ class HomeViewController: TableViewController { } func updateNav(navBarBgAlpha: CGFloat) { + self.navBarBgAlpha = navBarBgAlpha self.currentNavBarBgAlpha = navBarBgAlpha // 创建一个UINavigationBarAppearance的实例 @@ -273,7 +278,7 @@ extension HomeViewController { } -extension HomeViewController: NavigationBarTouchDelegate { +extension HomeViewController { func scrollViewDidScroll(_ scrollView: UIScrollView) { var delta = scrollView.contentOffset.y / CGFloat(BaseDimensions.topHeight) delta = CGFloat.maximum(delta, 0) @@ -282,16 +287,4 @@ extension HomeViewController: NavigationBarTouchDelegate { } - func navigationBar(_ navigationBar: NavigationBar, hitTest point: CGPoint, with event: UIEvent?) { - - let pagerViewPoint = view.convert(point, to: homePagerView) - if homePagerView.point(inside: pagerViewPoint, with: event) { - homePagerView.pagerView.selectItem(at: homePagerView.pagerView.currentIndex, animated: true) - homePagerView.pagerView.delegate?.pagerView?(homePagerView.pagerView, didHighlightItemAt: homePagerView.pagerView.currentIndex) - homePagerView.pagerView.delegate?.pagerView?(homePagerView.pagerView, didSelectItemAt: homePagerView.pagerView.currentIndex) - } - print("导航栏点击") - - } - }