TabBarView: animations!

This commit is contained in:
James Magahern
2021-02-11 20:25:16 -08:00
parent 8a39176414
commit 94b0f4348c
2 changed files with 29 additions and 6 deletions

View File

@@ -13,6 +13,7 @@ class DocumentControlViewController: UIViewController
let fontSizeAdjustView = FontSizeAdjustView() let fontSizeAdjustView = FontSizeAdjustView()
let findOnPageControlView = DocumentControlView() let findOnPageControlView = DocumentControlView()
let navigationControlView = NavigationControlsView() let navigationControlView = NavigationControlsView()
let settingsView = DocumentControlView()
var observations: [NSKeyValueObservation] = [] var observations: [NSKeyValueObservation] = []
@@ -24,9 +25,13 @@ class DocumentControlViewController: UIViewController
findOnPageControlView.label.text = "Find On Page" findOnPageControlView.label.text = "Find On Page"
findOnPageControlView.imageView.image = UIImage(systemName: "magnifyingglass") findOnPageControlView.imageView.image = UIImage(systemName: "magnifyingglass")
settingsView.label.text = "Settings"
settingsView.imageView.image = UIImage(systemName: "gear")
documentControlView.addArrangedSubview(navigationControlView) documentControlView.addArrangedSubview(navigationControlView)
documentControlView.addArrangedSubview(fontSizeAdjustView) documentControlView.addArrangedSubview(fontSizeAdjustView)
documentControlView.addArrangedSubview(findOnPageControlView) documentControlView.addArrangedSubview(findOnPageControlView)
documentControlView.addArrangedSubview(settingsView)
for (i, view) in documentControlView.arrangedSubviews.enumerated() { for (i, view) in documentControlView.arrangedSubviews.enumerated() {
view.drawsBottomSeparator = (i < documentControlView.arrangedSubviews.count - 1) view.drawsBottomSeparator = (i < documentControlView.arrangedSubviews.count - 1)

View File

@@ -10,6 +10,7 @@ import UIKit
class TabView: UIControl class TabView: UIControl
{ {
var active: Bool = false { didSet { setNeedsLayout() } } var active: Bool = false { didSet { setNeedsLayout() } }
var collapsed: Bool = false
let label = UILabel(frame: .zero) let label = UILabel(frame: .zero)
let closeButton = UIButton(frame: .zero) let closeButton = UIButton(frame: .zero)
let imageView = UIImageView(image: nil) let imageView = UIImageView(image: nil)
@@ -139,7 +140,7 @@ class TabBarView: UIView
bottomSeparatorView.backgroundColor = .systemFill bottomSeparatorView.backgroundColor = .systemFill
} }
public func reloadTabs() { public func reloadTabs(animated: Bool = true) {
guard let dataSource = self.dataSource else { return } guard let dataSource = self.dataSource else { return }
let numberOfTabs = dataSource.numberOfTabs(forTabBarView: self) let numberOfTabs = dataSource.numberOfTabs(forTabBarView: self)
@@ -149,14 +150,27 @@ class TabBarView: UIView
} }
while numberOfTabs > tabViews.count { while numberOfTabs > tabViews.count {
tabViews.append(makeTabView()) let newTabView = makeTabView()
if animated { newTabView.collapsed = true }
tabViews.append(newTabView)
} }
for (i, _) in tabViews.enumerated() { for (i, _) in tabViews.enumerated() {
self.reloadTab(atIndex: i) 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) { public func reloadTab(atIndex index: Int) {
@@ -209,7 +223,7 @@ class TabBarView: UIView
let minimumTabWidth = { (traitCollection: UITraitCollection) -> CGFloat in let minimumTabWidth = { (traitCollection: UITraitCollection) -> CGFloat in
if traitCollection.horizontalSizeClass == .compact { if traitCollection.horizontalSizeClass == .compact {
return tabContainerBounds.width / 3.0 return (tabContainerBounds.width / 3.0) + 15.0
} else { } else {
return 140.0 return 140.0
} }
@@ -217,8 +231,10 @@ class TabBarView: UIView
let maximumTabWidth = tabContainerBounds.width let maximumTabWidth = tabContainerBounds.width
let visibleTabCount = tabViews.filter({ $0.collapsed == false }).count
var xOffset = CGFloat(0.0) var xOffset = CGFloat(0.0)
var tabWidth: CGFloat = (tabContainerBounds.width / CGFloat(tabViews.count)) var tabWidth: CGFloat = (tabContainerBounds.width / CGFloat(visibleTabCount))
if tabWidth < minimumTabWidth { if tabWidth < minimumTabWidth {
tabWidth = minimumTabWidth tabWidth = minimumTabWidth
} else if tabWidth > maximumTabWidth { } else if tabWidth > maximumTabWidth {
@@ -231,7 +247,7 @@ class TabBarView: UIView
tabView.frame = CGRect( tabView.frame = CGRect(
x: xOffset, x: xOffset,
y: tabContainerBounds.minY, y: tabContainerBounds.minY,
width: tabWidth, width: tabView.collapsed ? 1.0 : tabWidth,
height: tabContainerBounds.height height: tabContainerBounds.height
) )
@@ -242,6 +258,8 @@ class TabBarView: UIView
} else { } else {
tabView.active = false tabView.active = false
} }
tabView.layoutIfNeeded()
} }
tabContainerView.contentSize = CGSize( tabContainerView.contentSize = CGSize(