Skip to content

Commit

Permalink
fixup scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
kahayash authored and kahayash committed Jul 17, 2017
1 parent 527a239 commit e7e6f8e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
7 changes: 6 additions & 1 deletion PagingKit/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ public class PagingMenuView: UIScrollView {
}

public func rectForItem(at index: Int) -> CGRect? {
guard index < frameQueue.count else { return nil }
guard index < frameQueue.count else {
let lastFrame = frameQueue.last
let rightEdge = lastFrame.flatMap { CGRect(x: $0.maxX, y: 0, width: 0, height: 0) }
return rightEdge
}

let x = (0..<index).reduce(0) { (sum, idx) in
return sum + frameQueue[idx].width
}
Expand Down
21 changes: 16 additions & 5 deletions PagingKit/PagingContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ public class PagingContentViewController: UIViewController {
public func scroll(to page: Int, animated: Bool) {
let offsetX = scrollView.bounds.width * CGFloat(page)
loadPagesIfNeeded(page: page)
UIView.perform(.delete, on: [], options: UIViewAnimationOptions(rawValue: 0),
animations: { [weak self] in
if animated {
performSystemAnimation({ [weak self] in
self?.scrollView.contentOffset = CGPoint(x: offsetX, y: 0)
},
completion: { _ in }
)
})
} else {
scrollView.contentOffset = CGPoint(x: offsetX, y: 0)
}
}

fileprivate var numberOfPages: Int = 0
Expand Down Expand Up @@ -209,3 +210,13 @@ extension PagingContentViewController: UIScrollViewDelegate {
loadScrollView(with: loadingPage + 1)
}
}

private func performSystemAnimation(_ animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil) {
UIView.perform(
.delete,
on: [],
options: UIViewAnimationOptions(rawValue: 0),
animations: animations,
completion: completion
)
}
4 changes: 2 additions & 2 deletions PagingKit/PagingMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public class PagingMenuViewController: UIViewController {

public func scroll(index: Int, percent: CGFloat = 0, animated: Bool = true) {
let rightIndex = index + 1
guard rightIndex < menuView.numberOfItem,
let leftFrame = menuView.rectForItem(at: index),

guard let leftFrame = menuView.rectForItem(at: index),
let rightFrame = menuView.rectForItem(at: rightIndex) else { return }

let width = (rightFrame.width - leftFrame.width) * percent + leftFrame.width
Expand Down
2 changes: 0 additions & 2 deletions iOS Sample/iOS Sample/MenuCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import UIKit
import PagingKit

class MenuCell: PagingMenuCell {

static let sizingCell = UINib(nibName: "MenuCell", bundle: nil).instantiate(withOwner: self, options: nil).first as! MenuCell

@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
Expand Down
12 changes: 4 additions & 8 deletions iOS Sample/iOS Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ViewController: UIViewController {
var contentViewController: PagingContentViewController?


let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let title = $0
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
vc.number = title
Expand All @@ -27,8 +27,8 @@ class ViewController: UIViewController {

menuViewController?.register(nib: UINib(nibName: "MenuCell", bundle: nil), forCellWithReuseIdentifier: "identifier")
menuViewController?.registerFocusView(nib: UINib(nibName: "FocusView", bundle: nil))
menuViewController?.reloadDate(startingOn: 0)
contentViewController?.reloadData(with: 0)
menuViewController?.reloadDate(startingOn: 2)
contentViewController?.reloadData(with: 2)
}

override func didReceiveMemoryWarning() {
Expand Down Expand Up @@ -60,11 +60,7 @@ extension ViewController: PagingMenuViewControllerDataSource {
}

func menuViewController(viewController: PagingMenuViewController, areaForItemAt index: Int) -> CGFloat {
MenuCell.sizingCell.titleLabel.text = dataSource[index].menu
var referenceSize = UILayoutFittingCompressedSize
referenceSize.height = viewController.view.bounds.height
let size = MenuCell.sizingCell.systemLayoutSizeFitting(referenceSize)
return size.width
return viewController.view.bounds.size.width / CGFloat(dataSource.count)
}


Expand Down

0 comments on commit e7e6f8e

Please sign in to comment.