update tabs model
This commit is contained in:
parent
320976e5ae
commit
3b23101710
@ -31,6 +31,8 @@ import UIKit
|
|||||||
var bottomLineLeftConstraint: NSLayoutConstraint?
|
var bottomLineLeftConstraint: NSLayoutConstraint?
|
||||||
var bottomLineWidthConstraint: NSLayoutConstraint?
|
var bottomLineWidthConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
|
private var widthLabel = Label()
|
||||||
|
|
||||||
//delegate
|
//delegate
|
||||||
weak public var delegate: TabsDelegate?
|
weak public var delegate: TabsDelegate?
|
||||||
|
|
||||||
@ -186,11 +188,14 @@ extension Tabs: UICollectionViewDelegateFlowLayout {
|
|||||||
return CGSize(width: getLabelWidth(labelModel).width, height: cellHeight)
|
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 {
|
func getLabelWidth(_ labelModel: LabelModel?) -> CGSize {
|
||||||
guard let labelModel = labelModel else { return .zero}
|
guard let labelModel = labelModel else { return .zero}
|
||||||
let label = Label()
|
widthLabel.set(with: labelModel, nil, nil)
|
||||||
label.set(with: labelModel, nil, nil)
|
let cgSize = widthLabel.intrinsicContentSize
|
||||||
return label.intrinsicContentSize
|
widthLabel.reset()
|
||||||
|
return cgSize
|
||||||
}
|
}
|
||||||
|
|
||||||
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
||||||
@ -244,6 +249,10 @@ extension Tabs: UICollectionViewDelegateFlowLayout {
|
|||||||
|
|
||||||
extension Tabs: UIScrollViewDelegate {
|
extension Tabs: UIScrollViewDelegate {
|
||||||
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
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 }
|
guard let offsetX = collectionView?.contentOffset.x else { return }
|
||||||
bottomScrollView.setContentOffset(CGPoint(x: offsetX, y: bottomScrollView.contentOffset.y), animated: false)
|
bottomScrollView.setContentOffset(CGPoint(x: offsetX, y: bottomScrollView.contentOffset.y), animated: false)
|
||||||
}
|
}
|
||||||
@ -272,46 +281,14 @@ extension Tabs {
|
|||||||
animationBlock()
|
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: CollectionViewCell {
|
||||||
|
|
||||||
@objcMembers public class TabItemCell: UICollectionViewCell {
|
|
||||||
public let label = Label()
|
public let label = Label()
|
||||||
public var labelModel: LabelModel?
|
public var labelModel: LabelModel?
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
public override func setupView() {
|
||||||
super.init(frame: frame)
|
super.setupView()
|
||||||
setupView()
|
|
||||||
}
|
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
|
||||||
super.init(coder: coder)
|
|
||||||
setupView()
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupView() {
|
|
||||||
contentView.addSubview(label)
|
contentView.addSubview(label)
|
||||||
NSLayoutConstraint.constraintPinSubview(label, pinTop: false, pinBottom: false, pinLeft: true, pinRight: true)
|
NSLayoutConstraint.constraintPinSubview(label, pinTop: false, pinBottom: false, pinLeft: true, pinRight: true)
|
||||||
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
|
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user