Skip to content

Commit

Permalink
Merge pull request #99 from kazuhiro4949/feature/add_calculate_underline
Browse files Browse the repository at this point in the history
add flexible underline and UnderlineFocusView
  • Loading branch information
kazuhiro4949 committed Sep 16, 2019
2 parents d9ac9cf + 2f5522e commit df84f0a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
22 changes: 16 additions & 6 deletions PagingKit/TitleLabelMenuViewCell.swift
Expand Up @@ -48,13 +48,22 @@ public class TitleLabelMenuViewCell: PagingMenuViewCell {
}
}

public var labelWidth: CGFloat {
return titleLabel.bounds.width
}

public let titleLabel = { () -> UILabel in
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 17)
label.textAlignment = .center
return label
}()

public func calcIntermediateLabelSize(with leftCell: TitleLabelMenuViewCell, percent: CGFloat) -> CGFloat {
let diff = (labelWidth - leftCell.labelWidth) * percent
return leftCell.labelWidth + diff
}

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
Expand All @@ -71,9 +80,10 @@ public class TitleLabelMenuViewCell: PagingMenuViewCell {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
addConstraints([
anchorLabel(from: titleLabel, to: self, attribute: .top),
anchorLabel(from: titleLabel, to: self, attribute: .leading),
anchorLabel(from: self, to: titleLabel, attribute: .trailing),
anchorLabel(from: self, to: titleLabel, attribute: .bottom)
anchorLabel(from: titleLabel, to: self, attribute: .leading, .greaterThanOrEqual),
anchorLabel(from: self, to: titleLabel, attribute: .trailing, .greaterThanOrEqual),
anchorLabel(from: self, to: titleLabel, attribute: .bottom),
anchorLabel(from: titleLabel, to: self, 0, attribute: .centerX)
])
}

Expand All @@ -89,15 +99,15 @@ public class TitleLabelMenuViewCell: PagingMenuViewCell {


/// syntax sugar of NSLayoutConstraint for titleLabel (Because this library supports iOS8, it cannnot use NSLayoutAnchor.)
private func anchorLabel(from fromItem: Any, to toItem: Any, attribute: NSLayoutConstraint.Attribute) -> NSLayoutConstraint {
private func anchorLabel(from fromItem: Any, to toItem: Any, _ constant: CGFloat = 8, attribute: NSLayoutConstraint.Attribute, _ relatedBy: NSLayoutConstraint.Relation = .equal) -> NSLayoutConstraint {
return NSLayoutConstraint(
item: fromItem,
attribute: attribute,
relatedBy: .equal,
relatedBy: relatedBy,
toItem: toItem,
attribute: attribute,
multiplier: 1,
constant: 8
constant: constant
)
}
}
Expand Down
42 changes: 39 additions & 3 deletions PagingKit/UnderlineFocusView.swift
Expand Up @@ -43,22 +43,50 @@ public class UnderlineFocusView: UIView {
}
}

public var underlineWidth: CGFloat? = nil {
didSet {
if let underlineWidth = underlineWidth {
widthConstraint.isActive = true
widthConstraint.constant = underlineWidth
} else {
widthConstraint.isActive = false
}
}
}

private let widthConstraint: NSLayoutConstraint
private let heightConstraint: NSLayoutConstraint
private let underlineView = UIView()

required public init?(coder aDecoder: NSCoder) {
widthConstraint = NSLayoutConstraint(
item: underlineView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .height, multiplier: 1, constant: 0
)

heightConstraint = NSLayoutConstraint(
item: underlineView,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height, multiplier: 1, constant: underlineHeight
attribute: .height, multiplier: 1, constant: 0
)
super.init(coder: aDecoder)
setup()
}

override public init(frame: CGRect) {
widthConstraint = NSLayoutConstraint(
item: underlineView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .height, multiplier: 1, constant: underlineHeight
)

heightConstraint = NSLayoutConstraint(
item: underlineView,
attribute: .height,
Expand All @@ -74,8 +102,16 @@ public class UnderlineFocusView: UIView {
addSubview(underlineView)
underlineView.translatesAutoresizingMaskIntoConstraints = false
addConstraint(heightConstraint)
let constraints = [.bottom, .leading, .trailing].anchor(from: underlineView, to: self)
addConstraints(constraints)
addConstraint(widthConstraint)
widthConstraint.isActive = false

let constraintsA = [.bottom].anchor(from: underlineView, to: self)
let constraintsB = [.width, .centerX].anchor(from: underlineView, to: self)
constraintsB.forEach {
$0.priority = .defaultHigh
$0.isActive = true
}
addConstraints(constraintsA + constraintsB)
underlineView.backgroundColor = underlineColor
}
}
17 changes: 15 additions & 2 deletions iOS Sample/iOS Sample/SimpleViewController.swift
Expand Up @@ -30,6 +30,8 @@ class SimpleViewController: UIViewController {
var menuViewController: PagingMenuViewController?
var contentViewController: PagingContentViewController?

let focusView = UnderlineFocusView()


let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let title = $0
Expand All @@ -39,15 +41,17 @@ class SimpleViewController: UIViewController {

lazy var firstLoad: (() -> Void)? = { [weak self, menuViewController, contentViewController] in
menuViewController?.reloadData()
contentViewController?.reloadData()
contentViewController?.reloadData { [weak self] in
self?.adjustfocusViewWidth(index: 0, percent: 0)
}
self?.firstLoad = nil
}

override func viewDidLoad() {
super.viewDidLoad()

menuViewController?.register(type: TitleLabelMenuViewCell.self, forCellWithReuseIdentifier: "identifier")
menuViewController?.registerFocusView(view: UnderlineFocusView())
menuViewController?.registerFocusView(view: focusView)
contentViewController?.scrollView.bounces = true
}
override func viewDidLayoutSubviews() {
Expand Down Expand Up @@ -115,6 +119,15 @@ extension SimpleViewController: PagingMenuViewControllerDelegate {
extension SimpleViewController: PagingContentViewControllerDelegate {
func contentViewController(viewController: PagingContentViewController, didManualScrollOn index: Int, percent: CGFloat) {
menuViewController?.scroll(index: index, percent: percent, animated: false)
adjustfocusViewWidth(index: index, percent: percent)
}

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 {
return
}
focusView.underlineWidth = rightCell.calcIntermediateLabelSize(with: leftCell, percent: percent)
}
}

0 comments on commit df84f0a

Please sign in to comment.