fix tabs to respect proper spacing

This commit is contained in:
Scott Pfeil 2021-12-17 15:47:38 -05:00
parent ba16e241cb
commit 5aaf93186a

View File

@ -45,8 +45,7 @@ import UIKit
//constant //constant
let TabCellId = "TabCell" let TabCellId = "TabCell"
public let sectionPadding: CGFloat = 20.0 public let sectionPadding: CGFloat = 20.0
public let cellSpacing: CGFloat = 34.0 public let cellHeight: CGFloat = 28.0
public let cellHeight: CGFloat = 27.0
public let selectionLineHeight: CGFloat = 4.0 public let selectionLineHeight: CGFloat = 4.0
public let selectionLineWidth: CGFloat = 32.0 public let selectionLineWidth: CGFloat = 32.0
public let selectionLineMovingTime: TimeInterval = 0.2 public let selectionLineMovingTime: TimeInterval = 0.2
@ -190,7 +189,7 @@ extension Tabs: UICollectionViewDelegateFlowLayout {
guard let labelModel = tabsModel?.tabs[indexPath.row].label else { guard let labelModel = tabsModel?.tabs[indexPath.row].label else {
return .zero return .zero
} }
return CGSize(width: getLabelWidth(labelModel).width, height: cellHeight) return CGSize(width: max(selectionLineWidth, getLabelWidth(labelModel).width), height: cellHeight)
} }
//pre calculate the width of the collection cell //pre calculate the width of the collection cell
@ -282,12 +281,10 @@ extension Tabs {
guard let collect = collectionView else {return} guard let collect = collectionView else {return}
let size = collectionView(collect, layout: layout, sizeForItemAt: indexPath) let size = collectionView(collect, layout: layout, sizeForItemAt: indexPath)
let barWidth = getLineWidth(for: indexPath)
let animationBlock = { let animationBlock = {
[weak self] in [weak self] in
let x = cell.frame.origin.x self?.selectionLineWidthConstraint?.constant = size.width
self?.selectionLineWidthConstraint?.constant = barWidth self?.selectionLineLeftConstraint?.constant = cell.frame.origin.x
self?.selectionLineLeftConstraint?.constant = x + (size.width - barWidth) / 2.0
self?.bottomContentView.layoutIfNeeded() self?.bottomContentView.layoutIfNeeded()
} }
if animated { if animated {
@ -296,33 +293,21 @@ extension Tabs {
animationBlock() animationBlock()
} }
} }
func getLineWidth(for indexPath: IndexPath) -> CGFloat {
guard let collection = collectionView else { return 0 }
let width = collectionView(collection, layout: layout, sizeForItemAt: indexPath).width
guard collectionView(collection, numberOfItemsInSection: indexPath.section) != 2 else {
return width
}
return max(width, selectionLineWidth)
}
/// Adjust the line based on the percentage of the transition.
func progress(from index: Int, toIndex: Int, percentage: CGFloat) { func progress(from index: Int, toIndex: Int, percentage: CGFloat) {
let fromIndexPath = IndexPath(row: index, section: 0) let fromIndexPath = IndexPath(row: index, section: 0)
let toIndexPath = IndexPath(row: toIndex, section: 0) let toIndexPath = IndexPath(row: toIndex, section: 0)
guard let collection = collectionView, guard let collection = collectionView,
let fromCell = collection.cellForItem(at: fromIndexPath), let fromCell = collection.cellForItem(at: fromIndexPath),
let toCell = collection.cellForItem(at: toIndexPath) else { return } let toCell = collection.cellForItem(at: toIndexPath) else { return }
let fromLineWidth = getLineWidth(for: fromIndexPath) // setting the width for percentage
let toLineWidth = getLineWidth(for: toIndexPath) selectionLineWidthConstraint?.constant = (toCell.bounds.width - fromCell.bounds.width) * percentage + fromCell.bounds.width
let finalLineWidth = (toLineWidth - fromLineWidth) * percentage + fromLineWidth
selectionLineWidthConstraint?.constant = finalLineWidth
// setting the x for percentage // setting the x for percentage
let fromCellWidth = collectionView(collection, layout: layout, sizeForItemAt: fromIndexPath).width let originalX = fromCell.frame.origin.x
let toCellWidth = collectionView(collection, layout: layout, sizeForItemAt: toIndexPath).width let toX = toCell.frame.origin.x
let originalX = fromCell.frame.origin.x + ((fromCellWidth - fromLineWidth) / 2.0)
let toX = toCell.frame.origin.x + ((toCellWidth - toLineWidth) / 2.0)
let xDifference = toX - originalX let xDifference = toX - originalX
let finalX = (xDifference * percentage) + originalX let finalX = (xDifference * percentage) + originalX
selectionLineLeftConstraint?.constant = finalX selectionLineLeftConstraint?.constant = finalX