From 94b0f4348ccf9a267027418198342033e620afd3 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Thu, 11 Feb 2021 20:25:16 -0800 Subject: [PATCH] TabBarView: animations! --- .../DocumentControlViewController.swift | 5 ++++ App/Tabs/TabBarView.swift | 30 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/App/Document Controls UI/DocumentControlViewController.swift b/App/Document Controls UI/DocumentControlViewController.swift index 01963dc..dd6102c 100644 --- a/App/Document Controls UI/DocumentControlViewController.swift +++ b/App/Document Controls UI/DocumentControlViewController.swift @@ -13,6 +13,7 @@ class DocumentControlViewController: UIViewController let fontSizeAdjustView = FontSizeAdjustView() let findOnPageControlView = DocumentControlView() let navigationControlView = NavigationControlsView() + let settingsView = DocumentControlView() var observations: [NSKeyValueObservation] = [] @@ -24,9 +25,13 @@ class DocumentControlViewController: UIViewController findOnPageControlView.label.text = "Find On Page" findOnPageControlView.imageView.image = UIImage(systemName: "magnifyingglass") + settingsView.label.text = "Settings" + settingsView.imageView.image = UIImage(systemName: "gear") + documentControlView.addArrangedSubview(navigationControlView) documentControlView.addArrangedSubview(fontSizeAdjustView) documentControlView.addArrangedSubview(findOnPageControlView) + documentControlView.addArrangedSubview(settingsView) for (i, view) in documentControlView.arrangedSubviews.enumerated() { view.drawsBottomSeparator = (i < documentControlView.arrangedSubviews.count - 1) diff --git a/App/Tabs/TabBarView.swift b/App/Tabs/TabBarView.swift index 6ebc987..6a72097 100644 --- a/App/Tabs/TabBarView.swift +++ b/App/Tabs/TabBarView.swift @@ -10,6 +10,7 @@ import UIKit class TabView: UIControl { var active: Bool = false { didSet { setNeedsLayout() } } + var collapsed: Bool = false let label = UILabel(frame: .zero) let closeButton = UIButton(frame: .zero) let imageView = UIImageView(image: nil) @@ -139,7 +140,7 @@ class TabBarView: UIView bottomSeparatorView.backgroundColor = .systemFill } - public func reloadTabs() { + public func reloadTabs(animated: Bool = true) { guard let dataSource = self.dataSource else { return } let numberOfTabs = dataSource.numberOfTabs(forTabBarView: self) @@ -149,14 +150,27 @@ class TabBarView: UIView } while numberOfTabs > tabViews.count { - tabViews.append(makeTabView()) + let newTabView = makeTabView() + if animated { newTabView.collapsed = true } + tabViews.append(newTabView) } for (i, _) in tabViews.enumerated() { self.reloadTab(atIndex: i) } - setNeedsLayout() + layoutSubviews() + + if animated { + tabViews.forEach { $0.collapsed = false } + UIView.animate(withDuration: 0.3) { [unowned self] in + layoutSubviews() + } + } + + // Adjust scroll offset + let contentEnd = CGPoint(x: tabContainerView.contentSize.width - tabContainerView.bounds.width, y: 0.0) + tabContainerView.setContentOffset(contentEnd, animated: animated) } public func reloadTab(atIndex index: Int) { @@ -209,7 +223,7 @@ class TabBarView: UIView let minimumTabWidth = { (traitCollection: UITraitCollection) -> CGFloat in if traitCollection.horizontalSizeClass == .compact { - return tabContainerBounds.width / 3.0 + return (tabContainerBounds.width / 3.0) + 15.0 } else { return 140.0 } @@ -217,8 +231,10 @@ class TabBarView: UIView let maximumTabWidth = tabContainerBounds.width + let visibleTabCount = tabViews.filter({ $0.collapsed == false }).count + var xOffset = CGFloat(0.0) - var tabWidth: CGFloat = (tabContainerBounds.width / CGFloat(tabViews.count)) + var tabWidth: CGFloat = (tabContainerBounds.width / CGFloat(visibleTabCount)) if tabWidth < minimumTabWidth { tabWidth = minimumTabWidth } else if tabWidth > maximumTabWidth { @@ -231,7 +247,7 @@ class TabBarView: UIView tabView.frame = CGRect( x: xOffset, y: tabContainerBounds.minY, - width: tabWidth, + width: tabView.collapsed ? 1.0 : tabWidth, height: tabContainerBounds.height ) @@ -242,6 +258,8 @@ class TabBarView: UIView } else { tabView.active = false } + + tabView.layoutIfNeeded() } tabContainerView.contentSize = CGSize(