Skip to content

Commit

Permalink
Merge pull request #46 from kazuhiro4949/feature/isSelected
Browse files Browse the repository at this point in the history
setting isSelected on PagingMenuView
  • Loading branch information
kazuhiro4949 authored Mar 1, 2018
2 parents 6d49e62 + c25d694 commit dd95ae2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
32 changes: 27 additions & 5 deletions PagingKit/PagingMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public class PagingMenuView: UIScrollView {
focusView.center = CGPoint(x: centerPointX, y: center.y)

contentOffset = CGPoint(x: normaizedOffsetX, y:0)
focusView.selectedIndex = index
focusView.selectedIndex = visibleCells.selectCell(to: focusView.center)
}

/// Scrolls a specific index of the menu so that it is visible in the receiver and calls handler when finishing scroll.
Expand All @@ -333,18 +333,17 @@ public class PagingMenuView: UIScrollView {
/// - completeHandler: handler called after completion
public func scroll(index: Int, completeHandler: @escaping (Bool) -> Void) {
let itemFrame = rectForItem(at: index)
focusView.selectedIndex = index

let offsetX = itemFrame.midX - bounds.width / 2
let offset = CGPoint(x: min(max(minContentOffsetX, offsetX), maxContentOffsetX), y: 0)

focusView.selectedIndex = visibleCells.selectCell(to: itemFrame.center)

UIView.perform(.delete, on: [], options: UIViewAnimationOptions(rawValue: 0), animations: { [weak self] in
self?.contentOffset = offset
self?.focusView.frame = itemFrame
self?.focusView.layoutIfNeeded()
}, completion: { (finish) in
completeHandler(finish)
})
}, completion:completeHandler)
}

// MARK:- Internal
Expand Down Expand Up @@ -589,3 +588,26 @@ private extension UIEdgeInsets {
return left + right
}
}

// MARK:- CGRect

private extension CGRect {
var center: CGPoint {
return CGPoint(x: midX, y: midY)
}
}

// MARK:- Array

private extension Array where Element == PagingMenuViewCell {
func resetSelected() {
forEach { $0.isSelected = false }
}

func selectCell(to point: CGPoint) -> Int? {
resetSelected()
let selectedCell = filter { $0.frame.contains(point) }.first
selectedCell?.isSelected = true
return selectedCell?.index
}
}
39 changes: 38 additions & 1 deletion PagingKitTests/PagingMenuViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,44 @@ class PagingMenuViewTests: XCTestCase {
0,
"not applying cellAlignment")
}

}

func testSelectedCell() {
guard let pagingMenuView = pagingMenuView else {
XCTFail("pagingMenuView is not nil")
return
}

let dataSource = MenuViewDataSourceSpy()
dataSource.widthForItem = 50
dataSource.registerNib(to: pagingMenuView)
dataSource.data = Array(repeating: "foo", count: 10)
pagingMenuView.dataSource = dataSource

pagingMenuView.reloadData()
pagingMenuView.scroll(index: 0)
XCTAssertEqual(
pagingMenuView.visibleCells.filter({ $0.isSelected }).first?.index,
0,
"updated isSelected on index 0")

pagingMenuView.scroll(index: 5)
XCTAssertEqual(
pagingMenuView.visibleCells.filter({ $0.isSelected }).first?.index,
5,
"updated isSelected on index 5")

pagingMenuView.scroll(index: 3, percent: 0.4)
XCTAssertEqual(
pagingMenuView.visibleCells.filter({ $0.isSelected }).first?.index,
3,
"updated isSelected on index 3")

pagingMenuView.scroll(index: 3, percent: 0.6)
XCTAssertEqual(
pagingMenuView.visibleCells.filter({ $0.isSelected }).first?.index,
4,
"updated isSelected on index 4")
}
}

Expand Down
6 changes: 0 additions & 6 deletions iOS Sample/iOS Sample/MenuReloadableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class MenuReloadableViewController: UIViewController {
extension MenuReloadableViewController: PagingMenuViewControllerDataSource {
func menuViewController(viewController: PagingMenuViewController, cellForItemAt index: Int) -> PagingMenuViewCell {
let cell = viewController.dequeueReusableCell(withReuseIdentifier: "identifier", for: index) as! TitleLabelMenuViewCell
cell.isSelected = (viewController.currentFocusedIndex == index)
cell.titleLabel.text = dataSource[index].menu
return cell
}
Expand Down Expand Up @@ -121,17 +120,12 @@ extension MenuReloadableViewController: PagingContentViewControllerDataSource {

extension MenuReloadableViewController: PagingMenuViewControllerDelegate {
func menuViewController(viewController: PagingMenuViewController, didSelect page: Int, previousPage: Int) {
viewController.visibleCells.forEach { $0.isSelected = false }
viewController.cellForItem(at: page)?.isSelected = true
contentViewController?.scroll(to: page, animated: true)
}
}

extension MenuReloadableViewController: PagingContentViewControllerDelegate {
func contentViewController(viewController: PagingContentViewController, didManualScrollOn index: Int, percent: CGFloat) {
let isRightCellSelected = percent > 0.5
menuViewController?.cellForItem(at: index)?.isSelected = !isRightCellSelected
menuViewController?.cellForItem(at: index + 1)?.isSelected = isRightCellSelected
menuViewController?.scroll(index: index, percent: percent, animated: false)
}
}
Expand Down
6 changes: 0 additions & 6 deletions iOS Sample/iOS Sample/SimpleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class SimpleViewController: UIViewController {
extension SimpleViewController: PagingMenuViewControllerDataSource {
func menuViewController(viewController: PagingMenuViewController, cellForItemAt index: Int) -> PagingMenuViewCell {
let cell = viewController.dequeueReusableCell(withReuseIdentifier: "identifier", for: index) as! TitleLabelMenuViewCell
cell.isSelected = (viewController.currentFocusedIndex == index)
cell.titleLabel.text = dataSource[index].menu
return cell
}
Expand Down Expand Up @@ -109,17 +108,12 @@ extension SimpleViewController: PagingContentViewControllerDataSource {

extension SimpleViewController: PagingMenuViewControllerDelegate {
func menuViewController(viewController: PagingMenuViewController, didSelect page: Int, previousPage: Int) {
viewController.visibleCells.forEach { $0.isSelected = false }
viewController.cellForItem(at: page)?.isSelected = true
contentViewController?.scroll(to: page, animated: true)
}
}

extension SimpleViewController: PagingContentViewControllerDelegate {
func contentViewController(viewController: PagingContentViewController, didManualScrollOn index: Int, percent: CGFloat) {
let isRightCellSelected = percent > 0.5
menuViewController?.cellForItem(at: index)?.isSelected = !isRightCellSelected
menuViewController?.cellForItem(at: index + 1)?.isSelected = isRightCellSelected
menuViewController?.scroll(index: index, percent: percent, animated: false)
}
}
Expand Down

0 comments on commit dd95ae2

Please sign in to comment.