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

@@ -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(