diff --git a/Sources/Menu Cells/Underline/TitleLabelMenuViewCell.swift b/Sources/Menu Cells/Underline/TitleLabelMenuViewCell.swift index 584e199..ace9f13 100644 --- a/Sources/Menu Cells/Underline/TitleLabelMenuViewCell.swift +++ b/Sources/Menu Cells/Underline/TitleLabelMenuViewCell.swift @@ -89,9 +89,9 @@ public class TitleLabelMenuViewCell: PagingMenuViewCell { } } - public func calcIntermediateLabelSize(with leftCell: TitleLabelMenuViewCell, percent: CGFloat) -> CGFloat { - let diff = (labelWidth - leftCell.labelWidth) * percent - return leftCell.labelWidth + diff + public func calcIntermediateLabelSize(with currentCell: TitleLabelMenuViewCell, percent: CGFloat) -> CGFloat { + let diff = (labelWidth - currentCell.labelWidth) * abs(percent) + return currentCell.labelWidth + diff } public required init?(coder aDecoder: NSCoder) { diff --git a/iOS Sample/iOS Sample/SimpleViewController.swift b/iOS Sample/iOS Sample/SimpleViewController.swift index 9f6cc47..7547db3 100644 --- a/iOS Sample/iOS Sample/SimpleViewController.swift +++ b/iOS Sample/iOS Sample/SimpleViewController.swift @@ -115,6 +115,13 @@ extension SimpleViewController: PagingMenuViewControllerDelegate { func menuViewController(viewController: PagingMenuViewController, didSelect page: Int, previousPage: Int) { contentViewController.scroll(to: page, animated: true) } + + func menuViewController(viewController: PagingMenuViewController, willAnimateFocusViewTo index: Int, with coordinator: PagingMenuFocusViewAnimationCoordinator) { + setFocusViewWidth(index: index) + coordinator.animateFocusView { [weak self] coordinator in + self?.focusView.layoutIfNeeded() + } completion: { _ in } + } } extension SimpleViewController: PagingContentViewControllerDelegate { @@ -123,12 +130,23 @@ extension SimpleViewController: PagingContentViewControllerDelegate { adjustfocusViewWidth(index: index, percent: percent) } + // TODO:- needs refactering func adjustfocusViewWidth(index: Int, percent: CGFloat) { - guard let leftCell = menuViewController.cellForItem(at: index) as? TitleLabelMenuViewCell, - let rightCell = menuViewController.cellForItem(at: index + 1) as? TitleLabelMenuViewCell else { + let adjucentIdx = percent < 0 ? index - 1 : index + 1 + guard let currentCell = menuViewController.cellForItem(at: index) as? TitleLabelMenuViewCell, + let adjucentCell = menuViewController.cellForItem(at: adjucentIdx) as? TitleLabelMenuViewCell else { + return + } + focusView.underlineWidth = adjucentCell.calcIntermediateLabelSize(with: currentCell, percent: percent) + } + + + // TODO:- needs refactering + func setFocusViewWidth(index: Int) { + guard let cell = menuViewController.cellForItem(at: index) as? TitleLabelMenuViewCell else { return } - focusView.underlineWidth = rightCell.calcIntermediateLabelSize(with: leftCell, percent: percent) + focusView.underlineWidth = cell.labelWidth } }