Skip to content

Commit

Permalink
Add didSelectMenuItem to PageMenuControllerDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
tamanyan committed Oct 18, 2017
1 parent f49a8f1 commit 6897543
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
4 changes: 4 additions & 0 deletions PageMenuExample/Sources/PageTabMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ extension PageTabMenuViewController: PageMenuControllerDelegate {
func pageMenuController(_ pageMenuController: PageMenuController, scrollingProgress progress: CGFloat, direction: PageMenuNavigationDirection) {
// The page view controller did complete scroll to a new page.
}

func pageMenuController(_ pageMenuController: PageMenuController, didSelectMenuItem index: Int, direction: PageMenuNavigationDirection) {
print("didSelectMenuItem \(index)")
}
}
5 changes: 5 additions & 0 deletions PageMenuExample/Sources/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,39 @@ class RootViewController: UITableViewController, UIGestureRecognizerDelegate {
items: items,
titles: tabTitles,
options: RoundRectPagerOption())

pageViewController.navigationItem.title = self.titles[indexPath.row]
self.navigationController?.pushViewController(pageViewController, animated: true)
} else if indexPath.row == 1 {
let pageViewController = PageTabMenuViewController(
items: items,
titles: tabTitles,
options: UnderlinePagerOption())

pageViewController.navigationItem.title = self.titles[indexPath.row]
self.navigationController?.pushViewController(pageViewController, animated: true)
} else if indexPath.row == 2 {
let pageViewController = PageTabMenuViewController(
items: items,
titles: tabTitles,
options: RoundRectPagerOption(isInfinite: true, tabMenuPosition: .custom))

pageViewController.navigationItem.title = self.titles[indexPath.row]
self.navigationController?.pushViewController(pageViewController, animated: true)
} else if indexPath.row == 3 {
let pageViewController = PageTabMenuViewController(
items: items,
titles: tabTitles,
options: RoundRectPagerOption(isInfinite: true))

pageViewController.navigationItem.title = self.titles[indexPath.row]
self.navigationController?.pushViewController(pageViewController, animated: true)
} else if indexPath.row == 4 {
let pageViewController = PageTabMenuViewController(
items: items,
titles: tabTitles,
options: UnderlinePagerOption(isInfinite: true))

pageViewController.navigationItem.title = self.titles[indexPath.row]
self.navigationController?.pushViewController(pageViewController, animated: true)
} else if indexPath.row == 5 {
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,23 @@ SwiftPageMenu give you the events below code.
@objc public protocol PageMenuControllerDelegate: class {
/// The page view controller will begin scrolling to a new page.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
willScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)
willScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)

/// The page view controller scroll progress between pages.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
scrollingProgress progress: CGFloat,
direction: PageMenuNavigationDirection)
scrollingProgress progress: CGFloat,
direction: PageMenuNavigationDirection)

/// The page view controller did complete scroll to a new page.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
didScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)
didScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)

/// The menu item of page view controller are selected.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
didSelectMenuItem index: Int,
direction: PageMenuNavigationDirection)
}
```

Expand Down
75 changes: 47 additions & 28 deletions Sources/PageMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ open class PageMenuController: UIViewController {
let tabView = TabMenuView(options: self.options)

tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: EMPageViewControllerNavigationDirection) in
self?.displayControllerWithIndex(index, direction: direction, animated: true)
guard let `self` = self else { return }

self.displayControllerWithIndex(index,
direction: direction,
animated: true)

self.delegate?.pageMenuController?(self,
didSelectMenuItem: index,
direction: direction.toPageMenuNavigationDirection)
}

return tabView
Expand Down Expand Up @@ -153,24 +161,27 @@ open class PageMenuController: UIViewController {
self.tabView.updateCollectionViewUserInteractionEnabled(false)

let completion: ((Bool) -> Void) = { [weak self] _ in
guard let this = self else {
return
}
guard let `self` = self else { return }

this.beforeIndex = index
this.pageViewController.delegate = this
this.tabView.updateCollectionViewUserInteractionEnabled(true)
this.delegate?.pageMenuController?(this, didScrollToPageAtIndex: index, direction: direction.toPageMenuNavigationDirection)
self.beforeIndex = index
self.pageViewController.delegate = self
self.tabView.updateCollectionViewUserInteractionEnabled(true)
self.delegate?.pageMenuController?(self,
didScrollToPageAtIndex: index,
direction: direction.toPageMenuNavigationDirection)
}

self.delegate?.pageMenuController?(self, willScrollToPageAtIndex: self.currentIndex ?? 0, direction: direction.toPageMenuNavigationDirection)
self.pageViewController.selectViewController(
viewControllers[index],
direction: direction,
animated: animated,
completion: completion)
self.delegate?.pageMenuController?(self,
willScrollToPageAtIndex: self.currentIndex ?? 0,
direction: direction.toPageMenuNavigationDirection)

self.pageViewController.selectViewController(viewControllers[index],
direction: direction,
animated: animated,
completion: completion)

guard self.isViewLoaded else { return }

self.tabView.updateCurrentIndex(index, shouldScroll: true)
}

Expand Down Expand Up @@ -214,11 +225,10 @@ open class PageMenuController: UIViewController {
return
}

self.pageViewController.selectViewController(
self.viewControllers[defaultIndex],
direction: .forward,
animated: false,
completion: nil)
self.pageViewController.selectViewController(self.viewControllers[defaultIndex],
direction: .forward,
animated: false,
completion: nil)
}

fileprivate func setup() {
Expand Down Expand Up @@ -303,13 +313,21 @@ open class PageMenuController: UIViewController {
// MARK:- EMPageViewControllerDelegate

extension PageMenuController: EMPageViewControllerDelegate {
func em_pageViewController(_ pageViewController: EMPageViewController, willStartScrollingFrom startingViewController: UIViewController, destinationViewController: UIViewController, direction: EMPageViewControllerNavigationDirection) {
func em_pageViewController(_ pageViewController: EMPageViewController,
willStartScrollingFrom startingViewController: UIViewController,
destinationViewController: UIViewController, direction: EMPageViewControllerNavigationDirection) {

// Order to prevent the the hit repeatedly during animation
self.tabView.updateCollectionViewUserInteractionEnabled(false)
self.delegate?.pageMenuController?(self, willScrollToPageAtIndex: self.currentIndex ?? 0, direction: direction.toPageMenuNavigationDirection)
}

func em_pageViewController(_ pageViewController: EMPageViewController, didFinishScrollingFrom startingViewController: UIViewController?, destinationViewController: UIViewController, direction: EMPageViewControllerNavigationDirection, transitionSuccessful: Bool) {
func em_pageViewController(_ pageViewController: EMPageViewController,
didFinishScrollingFrom startingViewController: UIViewController?,
destinationViewController: UIViewController,
direction: EMPageViewControllerNavigationDirection,
transitionSuccessful: Bool) {

if let currentIndex = self.currentIndex, currentIndex < self.tabItemCount {
self.tabView.updateCurrentIndex(currentIndex, shouldScroll: true)
self.beforeIndex = currentIndex
Expand All @@ -319,10 +337,13 @@ extension PageMenuController: EMPageViewControllerDelegate {
self.tabView.updateCollectionViewUserInteractionEnabled(true)
}

func em_pageViewController(_ pageViewController: EMPageViewController, isScrollingFrom startingViewController: UIViewController, destinationViewController: UIViewController?, direction: EMPageViewControllerNavigationDirection, progress: CGFloat) {
guard let beforeIndex = self.beforeIndex else {
return
}
func em_pageViewController(_ pageViewController: EMPageViewController,
isScrollingFrom startingViewController: UIViewController,
destinationViewController: UIViewController?,
direction: EMPageViewControllerNavigationDirection,
progress: CGFloat) {

guard let beforeIndex = self.beforeIndex else { return }

var index: Int

Expand All @@ -348,9 +369,7 @@ extension PageMenuController: EMPageViewControllerDelegate {

extension PageMenuController: EMPageViewControllerDataSource {
private func nextViewController(_ viewController: UIViewController, isAfter: Bool) -> UIViewController? {
guard var index = viewControllers.index(of: viewController) else {
return nil
}
guard var index = viewControllers.index(of: viewController) else { return nil }

if isAfter {
index += 1
Expand Down
17 changes: 11 additions & 6 deletions Sources/PageMenuControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import Foundation

/// The page view controller will begin scrolling to a new page.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
willScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)
willScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)

/// The page view controller scroll progress between pages.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
scrollingProgress progress: CGFloat,
direction: PageMenuNavigationDirection)
scrollingProgress progress: CGFloat,
direction: PageMenuNavigationDirection)

/// The page view controller did complete scroll to a new page.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
didScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)
didScrollToPageAtIndex index: Int,
direction: PageMenuNavigationDirection)

/// The menu item of page view controller are selected.
@objc optional func pageMenuController(_ pageMenuController: PageMenuController,
didSelectMenuItem index: Int,
direction: PageMenuNavigationDirection)
}

0 comments on commit 6897543

Please sign in to comment.