From 3b23101710b802566ad4492832e6c8a4f943fa27 Mon Sep 17 00:00:00 2001 From: "Xinlei(Ryan) Pan" Date: Mon, 27 Apr 2020 15:22:40 -0400 Subject: [PATCH] update tabs model --- .../HorizontalCombinationViews/Tabs.swift | 53 ++++++------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/Tabs.swift b/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/Tabs.swift index 6caa6a03..dc8b9cae 100644 --- a/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/Tabs.swift +++ b/MVMCoreUI/Atomic/Molecules/HorizontalCombinationViews/Tabs.swift @@ -31,6 +31,8 @@ import UIKit var bottomLineLeftConstraint: NSLayoutConstraint? var bottomLineWidthConstraint: NSLayoutConstraint? + private var widthLabel = Label() + //delegate weak public var delegate: TabsDelegate? @@ -186,11 +188,14 @@ extension Tabs: UICollectionViewDelegateFlowLayout { return CGSize(width: getLabelWidth(labelModel).width, height: cellHeight) } + //pre calculate the width of the collection cell + //when user select tabs, it will reload related collectionview, if we use autosize, it would relayout the width, need to keep the cell width constant. func getLabelWidth(_ labelModel: LabelModel?) -> CGSize { guard let labelModel = labelModel else { return .zero} - let label = Label() - label.set(with: labelModel, nil, nil) - return label.intrinsicContentSize + widthLabel.set(with: labelModel, nil, nil) + let cgSize = widthLabel.intrinsicContentSize + widthLabel.reset() + return cgSize } public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { @@ -244,6 +249,10 @@ extension Tabs: UICollectionViewDelegateFlowLayout { extension Tabs: UIScrollViewDelegate { public func scrollViewDidScroll(_ scrollView: UIScrollView) { + /*bottomScrollview is subview of self, it's not belongs to collectionview. + When collectionview is scrolling, bottomScrollView will stay without moving + Adding collectionview's offset to bottomScrollView, will make the bottomScrollview looks like scrolling with the selected tab item. + */ guard let offsetX = collectionView?.contentOffset.x else { return } bottomScrollView.setContentOffset(CGPoint(x: offsetX, y: bottomScrollView.contentOffset.y), animated: false) } @@ -272,46 +281,14 @@ extension Tabs { animationBlock() } } - - public func progress(fromIndex: Int, toIndex: Int, progressPercentage percent: CGFloat) { - guard let _ = collectionView else { return } - MVMCoreDispatchUtility.performBlock(onMainThread: { - self.setBottomLine(fromIndex: IndexPath(row: fromIndex, section: 0), toIndex: IndexPath(row: toIndex, section: 0), percent: percent) - }) - } - - func setBottomLine(fromIndex: IndexPath, toIndex: IndexPath, percent: CGFloat) { - guard let fromCell = collectionView?.cellForItem(at: fromIndex) as? TabItemCell, let toCell = collectionView?.cellForItem(at: toIndex) as? TabItemCell else { - return - } - let fromWidth = getLabelWidth(fromCell.labelModel).width - let toWidth = getLabelWidth(toCell.labelModel).width - let finalWidth = (toWidth - fromWidth) * percent + fromWidth - bottomLineWidthConstraint?.constant = finalWidth - let xDifference = toCell.frame.origin.x - fromCell.frame.origin.x - let finalX = xDifference * percent + fromCell.frame.origin.x - bottomLineLeftConstraint?.constant = finalX - bottomContentView.layoutIfNeeded() - } } - - -@objcMembers public class TabItemCell: UICollectionViewCell { +@objcMembers public class TabItemCell: CollectionViewCell { public let label = Label() public var labelModel: LabelModel? - override init(frame: CGRect) { - super.init(frame: frame) - setupView() - } - - required init?(coder: NSCoder) { - super.init(coder: coder) - setupView() - } - - func setupView() { + public override func setupView() { + super.setupView() contentView.addSubview(label) NSLayoutConstraint.constraintPinSubview(label, pinTop: false, pinBottom: false, pinLeft: true, pinRight: true) label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true