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
let TabCellId = "TabCell"
public let sectionPadding: CGFloat = 20.0
public let cellSpacing: CGFloat = 34.0
public let cellHeight: CGFloat = 27.0
public let cellHeight: CGFloat = 28.0
public let selectionLineHeight: CGFloat = 4.0
public let selectionLineWidth: CGFloat = 32.0
public let selectionLineMovingTime: TimeInterval = 0.2
@ -190,7 +189,7 @@ extension Tabs: UICollectionViewDelegateFlowLayout {
guard let labelModel = tabsModel?.tabs[indexPath.row].label else {
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
@ -282,12 +281,10 @@ extension Tabs {
guard let collect = collectionView else {return}
let size = collectionView(collect, layout: layout, sizeForItemAt: indexPath)
let barWidth = getLineWidth(for: indexPath)
let animationBlock = {
[weak self] in
let x = cell.frame.origin.x
self?.selectionLineWidthConstraint?.constant = barWidth
self?.selectionLineLeftConstraint?.constant = x + (size.width - barWidth) / 2.0
self?.selectionLineWidthConstraint?.constant = size.width
self?.selectionLineLeftConstraint?.constant = cell.frame.origin.x
self?.bottomContentView.layoutIfNeeded()
}
if animated {
@ -296,33 +293,21 @@ extension Tabs {
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) {
let fromIndexPath = IndexPath(row: index, section: 0)
let toIndexPath = IndexPath(row: toIndex, section: 0)
guard let collection = collectionView,
let fromCell = collection.cellForItem(at: fromIndexPath),
let toCell = collection.cellForItem(at: toIndexPath) else { return }
let fromLineWidth = getLineWidth(for: fromIndexPath)
let toLineWidth = getLineWidth(for: toIndexPath)
let finalLineWidth = (toLineWidth - fromLineWidth) * percentage + fromLineWidth
selectionLineWidthConstraint?.constant = finalLineWidth
// setting the width for percentage
selectionLineWidthConstraint?.constant = (toCell.bounds.width - fromCell.bounds.width) * percentage + fromCell.bounds.width
// setting the x for percentage
let fromCellWidth = collectionView(collection, layout: layout, sizeForItemAt: fromIndexPath).width
let toCellWidth = collectionView(collection, layout: layout, sizeForItemAt: toIndexPath).width
let originalX = fromCell.frame.origin.x + ((fromCellWidth - fromLineWidth) / 2.0)
let toX = toCell.frame.origin.x + ((toCellWidth - toLineWidth) / 2.0)
let originalX = fromCell.frame.origin.x
let toX = toCell.frame.origin.x
let xDifference = toX - originalX
let finalX = (xDifference * percentage) + originalX
selectionLineLeftConstraint?.constant = finalX