Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
kahayash authored and kahayash committed Jul 18, 2017
1 parent 869c60a commit a7ec1ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
29 changes: 19 additions & 10 deletions PagingKit/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,13 @@ public class PagingMenuView: UIScrollView {
guard let dataSource = dataSource else {
return
}

visibleCells.forEach { $0.removeFromSuperview() }
visibleCells = []

numberOfItem = dataSource.numberOfItemForPagingMenuView()

frameQueue = []
var containerWidth: CGFloat = 0
(0..<numberOfItem).forEach { (index) in
let width = dataSource.pagingMenuView(pagingMenuView: self, widthForItemAt: index)
frameQueue.append(CGRect(x: containerWidth, y: 0, width: width, height: bounds.height))
containerWidth += width
}
contentSize = CGSize(width: containerWidth, height: bounds.height)
containerView.frame = CGRect(origin: .zero, size: contentSize)
invalidateLayout()

setNeedsLayout()
layoutIfNeeded()
Expand Down Expand Up @@ -229,6 +221,23 @@ public class PagingMenuView: UIScrollView {
return CGRect(x: x, y: 0, width: frameQueue[index].width, height: bounds.height)
}

public func invalidateLayout() {
guard let dataSource = dataSource else {
return
}

frameQueue = []
var containerWidth: CGFloat = 0
(0..<numberOfItem).forEach { (index) in
let width = dataSource.pagingMenuView(pagingMenuView: self, widthForItemAt: index)
frameQueue.append(CGRect(x: containerWidth, y: 0, width: width, height: bounds.height))
containerWidth += width
}
contentSize = CGSize(width: containerWidth, height: bounds.height)
containerView.frame = CGRect(origin: .zero, size: contentSize)
align()
}

private func recenterIfNeeded() {
let currentOffset = contentOffset
let contentWidth = contentSize.width
Expand Down
6 changes: 2 additions & 4 deletions PagingKit/PagingContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class PagingContentViewController: UIViewController {
public func reloadData(with page: Int = 0) {
initialLoad(with: page)
scroll(to: page, animated: false)
leftSidePageIndex = page
}

public func scroll(to page: Int, animated: Bool) {
Expand All @@ -65,7 +66,6 @@ public class PagingContentViewController: UIViewController {

fileprivate var numberOfPages: Int = 0

fileprivate var lastContentOffset = CGPoint.zero
fileprivate var leftSidePageIndex = 0
fileprivate var isExplicityScrolling = false

Expand Down Expand Up @@ -157,10 +157,8 @@ extension PagingContentViewController: UIScrollViewDelegate {
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
lastContentOffset = scrollView.contentOffset
leftSidePageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)

if isExplicityScrolling {
leftSidePageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
let leftSideContentOffset = CGFloat(leftSidePageIndex) * scrollView.bounds.width
let percent = (scrollView.contentOffset.x - leftSideContentOffset) / scrollView.bounds.width
let normalizedPercent = min(max(0, percent), 1)
Expand Down
1 change: 1 addition & 0 deletions PagingKit/PagingMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public class PagingMenuViewController: UIViewController {

let index = menuView.indexForItem(at: focusView.center)
layoutHandler = { [weak self] in
self?.menuView.invalidateLayout()
self?.scroll(index: index ?? 0, percent: 0, animated: false)
self?.layoutHandler = nil
}
Expand Down
5 changes: 0 additions & 5 deletions iOS Sample/iOS Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class ViewController: UIViewController {
menuViewController?.reloadData(startingOn: dataSource.count - 1)
contentViewController?.reloadData(with: dataSource.count - 1)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
menuViewController?.reloadData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
Expand Down

0 comments on commit a7ec1ce

Please sign in to comment.