Skip to content
This repository has been archived by the owner on Jun 17, 2018. It is now read-only.

Reload PageMenu Title #379

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions Example/PagingMenuControllerDemo2/RootViewControoler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import UIKit
import PagingMenuController

private struct PagingMenuOptions: PagingMenuControllerCustomizable {
private let viewController1 = ViewController1()
private let viewController2 = ViewController2()


fileprivate var componentType: ComponentType {
return .all(menuOptions: MenuOptions(), pagingControllers: pagingControllers)
}

fileprivate var pagingControllers: [UIViewController] {
return [viewController1, viewController2]
}


var pagingControllers: [UIViewController]!

fileprivate struct MenuOptions: MenuViewCustomizable {
var displayMode: MenuDisplayMode {
return .segmentedControl
Expand All @@ -29,7 +25,6 @@ private struct PagingMenuOptions: PagingMenuControllerCustomizable {
return [MenuItem1(), MenuItem2()]
}
}

fileprivate struct MenuItem1: MenuItemViewCustomizable {
var displayMode: MenuItemDisplayMode {
return .text(title: MenuItemText(text: "First Menu"))
Expand All @@ -42,14 +37,50 @@ private struct PagingMenuOptions: PagingMenuControllerCustomizable {
}
}

private struct PagingMenuOptions1: PagingMenuControllerCustomizable {
private let viewController1 = ViewController1()
private let viewController2 = ViewController2()

fileprivate var componentType: ComponentType {
return .all(menuOptions: MenuOptions(), pagingControllers: pagingControllers)
}

var pagingControllers: [UIViewController]!

fileprivate struct MenuOptions: MenuViewCustomizable {
var displayMode: MenuDisplayMode {
return .segmentedControl
}
var itemsOptions: [MenuItemViewCustomizable] {
return [MenuItem1(), MenuItem2()]
}
}
fileprivate struct MenuItem1: MenuItemViewCustomizable {
var displayMode: MenuItemDisplayMode {
return .text(title: MenuItemText(text: "123"))
}
}
fileprivate struct MenuItem2: MenuItemViewCustomizable {
var displayMode: MenuItemDisplayMode {
return .text(title: MenuItemText(text: "456"))
}
}
}

class RootViewControoler: UIViewController {
var viewController1 = UIViewController()
var viewController2 = UIViewController()
var pagingMenuController: PagingMenuController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
viewController1.view.backgroundColor = .purple
viewController2.view.backgroundColor = .red
view.backgroundColor = UIColor.white

let options = PagingMenuOptions()
let pagingMenuController = PagingMenuController(options: options)
var options = PagingMenuOptions()
options.pagingControllers = [viewController1, viewController2]
pagingMenuController = PagingMenuController(options: options)
pagingMenuController.view.frame.origin.y += 64
pagingMenuController.view.frame.size.height -= 64
pagingMenuController.onMove = { state in
Expand All @@ -76,5 +107,18 @@ class RootViewControoler: UIViewController {
addChildViewController(pagingMenuController)
view.addSubview(pagingMenuController.view)
pagingMenuController.didMove(toParentViewController: self)

let rightBarButton = UIBarButtonItem(title: "Reload", style: .done, target: self, action: #selector(RootViewControoler.reload))
self.navigationItem.rightBarButtonItem = rightBarButton

let marker = UIView(frame: CGRect(x: 30, y: 30, width: 50, height: 50))
marker.backgroundColor = .blue
viewController1.view.addSubview(marker)
}

func reload() {
var options = PagingMenuOptions1()
options.pagingControllers = [viewController1, viewController2]
pagingMenuController.reload(options)
}
}
17 changes: 9 additions & 8 deletions PagingMenuController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = ECE5305D1B6DEB18001CF201;
Expand Down Expand Up @@ -223,12 +224,12 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -266,10 +267,10 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -287,12 +288,12 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = PagingMenuController/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -306,11 +307,11 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = PagingMenuController/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
3 changes: 1 addition & 2 deletions Pod/Classes/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ extension MenuItemView {

fileprivate func estimatedLabelSize(_ label: UILabel) -> CGSize {
guard let text = label.text else { return .zero }
return NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: label.font], context: nil).size
}
return NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: label.font ?? UIFont.systemFont(ofSize: label.font.pointSize)], context: nil).size }

fileprivate func calculateLabelSize(_ label: UILabel, maxWidth: CGFloat) -> CGSize {
guard let _ = label.text else { return .zero }
Expand Down
2 changes: 1 addition & 1 deletion Pod/Classes/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ open class MenuView: UIScrollView {
bounces = menuViewBounces
isScrollEnabled = menuViewScrollEnabled
isDirectionalLockEnabled = true
decelerationRate = menuOptions.deceleratingRate
decelerationRate = UIScrollView.DecelerationRate(rawValue: menuOptions.deceleratingRate)
scrollsToTop = false
translatesAutoresizingMaskIntoConstraints = false
}
Expand Down
56 changes: 44 additions & 12 deletions Pod/Classes/PagingMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ open class PagingMenuController: UIViewController {

pagingViewController.contentScrollView.delegate = self
view.addSubview(pagingViewController.view)
addChildViewController(pagingViewController)
pagingViewController.didMove(toParentViewController: self)
addChild(pagingViewController)
pagingViewController.didMove(toParent: self)
}
}
public var onMove: ((MenuMoveState) -> Void)? {
Expand Down Expand Up @@ -118,6 +118,38 @@ open class PagingMenuController: UIViewController {

move(toPage: currentPage, animated: false)
}

open func reload(_ options: PagingMenuControllerCustomizable) {
self.options = options
switch options.componentType {
case .all(let menuOptions, _):
self.menuOptions = menuOptions
case .menuView(let menuOptions):
self.menuOptions = menuOptions
default: break
}

// setupMenuView
switch options.componentType {
case .pagingController: return
default: break
}
// create a new menu
constructMenuView()
layoutMenuView()

// setupMenuController
switch options.componentType {
case .menuView: return
default: break
}

// create a new pagingViewController
constructPagingViewController()
layoutPagingViewController()

move(toPage: currentPage, animated: false)
}

fileprivate func setupMenuView() {
switch options.componentType {
Expand Down Expand Up @@ -478,10 +510,10 @@ extension PagingMenuController {
menuView?.addGestureRecognizer(rightSwipeGestureRecognizer)
}

internal func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
@objc internal func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
guard let menuItemView = recognizer.view as? MenuItemView,
let menuView = menuView,
let page = menuView.menuItemViews.index(of: menuItemView),
let menuView = menuView,
let page = menuView.menuItemViews.firstIndex(of: menuItemView),
page != menuView.currentPage,
let menuOptions = menuOptions else { return }

Expand All @@ -503,19 +535,19 @@ extension PagingMenuController {
move(toPage: newPage)
}

internal func handleSwipeGesture(_ recognizer: UISwipeGestureRecognizer) {
@objc internal func handleSwipeGesture(_ recognizer: UISwipeGestureRecognizer) {
guard let menuView = recognizer.view as? MenuView,
let menuOptions = menuOptions else { return }

let newPage: Int
switch (recognizer.direction, menuOptions.displayMode) {
case (UISwipeGestureRecognizerDirection.left, .infinite):
case (UISwipeGestureRecognizer.Direction.left, .infinite):
newPage = menuView.nextPage
case (UISwipeGestureRecognizerDirection.left, _):
case (UISwipeGestureRecognizer.Direction.left, _):
newPage = min(nextPage, menuOptions.itemsOptions.count - 1)
case (UISwipeGestureRecognizerDirection.right, .infinite):
case (UISwipeGestureRecognizer.Direction.right, .infinite):
newPage = menuView.previousPage
case (UISwipeGestureRecognizerDirection.right, _):
case (UISwipeGestureRecognizer.Direction.right, _):
newPage = max(previousPage, 0)
default: return
}
Expand All @@ -533,8 +565,8 @@ extension PagingMenuController {
if let pagingViewController = self.pagingViewController {
pagingViewController.cleanup()
pagingViewController.view.removeFromSuperview()
pagingViewController.removeFromParentViewController()
pagingViewController.willMove(toParentViewController: nil)
pagingViewController.removeFromParent()
pagingViewController.willMove(toParent: nil)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions Pod/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ open class PagingViewController: UIViewController {
if !shouldLoad(page: index) {
// remove unnecessary child view controllers
if isVisible(controller: controller) {
controller.willMove(toParentViewController: nil)
controller.willMove(toParent: nil)
controller.view!.removeFromSuperview()
controller.removeFromParentViewController()
controller.removeFromParent()

let _ = visibleControllers.index(of: controller).flatMap { visibleControllers.remove(at: $0) }
}
Expand All @@ -118,8 +118,8 @@ open class PagingViewController: UIViewController {
pagingView.translatesAutoresizingMaskIntoConstraints = false

contentScrollView.addSubview(pagingView)
addChildViewController(controller as UIViewController)
controller.didMove(toParentViewController: self)
addChild(controller as UIViewController)
controller.didMove(toParent: self)

visibleControllers.append(controller)
}
Expand Down Expand Up @@ -242,10 +242,10 @@ extension PagingViewController {
visibleControllers.removeAll(keepingCapacity: true)
currentViewController = nil

childViewControllers.forEach {
$0.willMove(toParentViewController: nil)
children.forEach {
$0.willMove(toParent: nil)
$0.view.removeFromSuperview()
$0.removeFromParentViewController()
$0.removeFromParent()
}

contentScrollView.removeFromSuperview()
Expand Down Expand Up @@ -276,7 +276,7 @@ extension PagingViewController {
}

fileprivate func isVisible(controller: UIViewController) -> Bool {
return self.childViewControllers.contains(controller)
return self.children.contains(controller)
}

fileprivate func hideVisibleControllers() {
Expand Down
2 changes: 1 addition & 1 deletion Pod/Classes/Protocols/MenuViewCustomizable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public extension MenuViewCustomizable {
return 0.3
}
var deceleratingRate: CGFloat {
return UIScrollViewDecelerationRateFast
return UIScrollView.DecelerationRate.fast.rawValue
}
var selectedItemCenter: Bool {
return true
Expand Down