Skip to content

Commit

Permalink
reduce waste
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuhiro Hayashi committed Oct 28, 2017
1 parent d05b0ef commit af2698d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions PagingKit/PagingMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,7 @@ public class PagingMenuView: UIScrollView {

public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(UIView.bounds), let newFrame = change?[.newKey] as? CGRect, let oldFrame = change?[.oldKey] as? CGRect, newFrame.height != oldFrame.height {
contentSize.height = newFrame.height
containerView.frame.size.height = newFrame.height
align()
if let selectedIndex = focusView.selectedIndex {
scroll(index: selectedIndex, animated: false, baseBounds: newFrame)
}
adjustComponentHeights(from: newFrame.height)
}
}

Expand All @@ -307,10 +302,9 @@ public class PagingMenuView: UIScrollView {
/// - index: A index defining an menu of the menu view.
/// - percent: A rate that transit from the index.
/// - animated: true if the scrolling should be animated, false if it should be immediate.
public func scroll(index: Int, percent: CGFloat = 0, animated: Bool = true, baseBounds: CGRect? = nil) {
/// - baseBounds: a rect base boounds to calculate position and size
public func scroll(index: Int, percent: CGFloat = 0, animated: Bool = true) {
let rightIndex = index + 1
let bounds = baseBounds ?? self.bounds
let centerY = baseBounds?.midY ?? self.center.y

guard let leftFrame = rectForItem(at: index),
let rightFrame = rectForItem(at: rightIndex) else { return }
Expand All @@ -321,7 +315,7 @@ public class PagingMenuView: UIScrollView {
let centerPointX = leftFrame.midX + (rightFrame.midX - leftFrame.midX) * percent
let offsetX = centerPointX - bounds.width / 2
let normaizedOffsetX = min(max(minContentOffsetX, offsetX), maxContentOffsetX)
focusView.center = CGPoint(x: centerPointX, y: centerY)
focusView.center = CGPoint(x: centerPointX, y: center.y)

setContentOffset(CGPoint(x: normaizedOffsetX, y:0), animated: animated)
focusView.selectedIndex = index
Expand Down Expand Up @@ -376,6 +370,13 @@ public class PagingMenuView: UIScrollView {
}
}

private func adjustComponentHeights(from newHeight: CGFloat) {
contentSize.height = newHeight
containerView.frame.size.height = newHeight
visibleCells.forEach { $0.frame.size.height = newHeight }
focusView.frame.size.height = newHeight
}

@discardableResult
private func placeNewCellOnRight(with rightEdge: CGFloat, index: Int, dataSource: PagingMenuViewDataSource) -> CGFloat {
let nextIndex = (index + 1) % numberOfItem
Expand Down

0 comments on commit af2698d

Please sign in to comment.