Skip to content

Commit

Permalink
fix underline size
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuhiro4949 committed May 22, 2021
1 parent fe554ed commit 9357d4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Sources/Menu Cells/Underline/TitleLabelMenuViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 21 additions & 3 deletions iOS Sample/iOS Sample/SimpleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}

0 comments on commit 9357d4a

Please sign in to comment.