Commit 2d7a1a66 by Daniel Dahan

development: issue-608: Added a feature to PageTabBarController that allows you…

development: issue-608: Added a feature to PageTabBarController that allows you to programmatically select an index easily.
parent 909c99d2
...@@ -362,12 +362,12 @@ ...@@ -362,12 +362,12 @@
name = Height; name = Height;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
962DDD071D6FBBB7001C307C /* Page */ = { 962DDD071D6FBBB7001C307C /* PageTabBar */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
963FBF071D669D14008F8512 /* PageTabBarController.swift */, 963FBF071D669D14008F8512 /* PageTabBarController.swift */,
); );
name = Page; name = PageTabBar;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
962DDD081D6FBBD0001C307C /* BottomTabBar */ = { 962DDD081D6FBBD0001C307C /* BottomTabBar */ = {
...@@ -527,7 +527,7 @@ ...@@ -527,7 +527,7 @@
96BCB8091CB4107700C806FE /* Motion */, 96BCB8091CB4107700C806FE /* Motion */,
96BCB8011CB40F1700C806FE /* Navigation */, 96BCB8011CB40F1700C806FE /* Navigation */,
961E6BEF1DDA4B04004E6C93 /* NavigationDrawer */, 961E6BEF1DDA4B04004E6C93 /* NavigationDrawer */,
962DDD071D6FBBB7001C307C /* Page */, 962DDD071D6FBBB7001C307C /* PageTabBar */,
96717B151DBE6B1800DA84DB /* Photos */, 96717B151DBE6B1800DA84DB /* Photos */,
96328B8F1E05B69A009A4C90 /* Reminders */, 96328B8F1E05B69A009A4C90 /* Reminders */,
9626CA951DAB5370003E2611 /* Root */, 9626CA951DAB5370003E2611 /* Root */,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>FMWK</string> <string>FMWK</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.6.1</string> <string>2.6.2</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -113,8 +113,18 @@ open class PageTabBarController: RootController { ...@@ -113,8 +113,18 @@ open class PageTabBarController: RootController {
/// Indicates that the tab has been pressed and animating. /// Indicates that the tab has been pressed and animating.
open internal(set) var isTabSelectedAnimation = false open internal(set) var isTabSelectedAnimation = false
/// An internal reference to the selectedIndex.
fileprivate var internalSelectedIndex = 0
/// The currently selected UIViewController. /// The currently selected UIViewController.
open internal(set) var selectedIndex = 0 open var selectedIndex: Int {
get {
return internalSelectedIndex
}
set(value) {
setInternalSelectedIndex(index: value, animated: true)
}
}
/// PageTabBar alignment setting. /// PageTabBar alignment setting.
open var pageTabBarAlignment = PageTabBarAlignment.bottom open var pageTabBarAlignment = PageTabBarAlignment.bottom
...@@ -155,16 +165,15 @@ open class PageTabBarController: RootController { ...@@ -155,16 +165,15 @@ open class PageTabBarController: RootController {
isBounceEnabled = true isBounceEnabled = true
super.init(rootViewController: UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)) super.init(rootViewController: UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil))
viewControllers.append(rootViewController) viewControllers.append(rootViewController)
setViewControllers(viewControllers, direction: .forward, animated: true) setInternalSelectedIndex(index: 0, animated: true)
prepare() prepare()
} }
public init(viewControllers: [UIViewController], selectedIndex: Int = 0) { public init(viewControllers: [UIViewController], selectedIndex index: Int = 0) {
isBounceEnabled = true isBounceEnabled = true
super.init(rootViewController: UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)) super.init(rootViewController: UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil))
self.selectedIndex = selectedIndex
self.viewControllers.append(contentsOf: viewControllers) self.viewControllers.append(contentsOf: viewControllers)
setViewControllers([self.viewControllers[selectedIndex]], direction: .forward, animated: true) setInternalSelectedIndex(index: index, animated: true)
prepare() prepare()
} }
...@@ -198,7 +207,6 @@ open class PageTabBarController: RootController { ...@@ -198,7 +207,6 @@ open class PageTabBarController: RootController {
*/ */
open func setViewControllers(_ viewControllers: [UIViewController], direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) { open func setViewControllers(_ viewControllers: [UIViewController], direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) {
pageViewController?.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion) pageViewController?.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion)
prepare()
} }
/** /**
...@@ -240,44 +248,65 @@ open class PageTabBarController: RootController { ...@@ -240,44 +248,65 @@ open class PageTabBarController: RootController {
button.addTarget(self, action: #selector(handlePageTabBarButton(button:)), for: .touchUpInside) button.addTarget(self, action: #selector(handlePageTabBarButton(button:)), for: .touchUpInside)
} }
} }
}
extension PageTabBarController {
/// Prepares the pageTabBar.
fileprivate func preparePageTabBar() {
pageTabBar.zPosition = 1000
pageTabBar.dividerColor = Color.grey.lighten3
view.addSubview(pageTabBar)
pageTabBar.select(at: internalSelectedIndex)
}
}
extension PageTabBarController {
/** /**
Handles the pageTabBarButton. Handles the pageTabBarButton.
- Parameter button: A UIButton. - Parameter button: A UIButton.
*/ */
@objc @objc
internal func handlePageTabBarButton(button: UIButton) { fileprivate func handlePageTabBarButton(button: UIButton) {
guard let index = pageTabBar.buttons.index(of: button) else { guard let index = pageTabBar.buttons.index(of: button) else {
return return
} }
guard index != selectedIndex else { guard index != internalSelectedIndex else {
return return
} }
let direction: UIPageViewControllerNavigationDirection = index < selectedIndex ? .reverse : .forward setInternalSelectedIndex(index: index, animated: true)
}
}
extension PageTabBarController {
/**
Internally sets the internalSelectedIndex value.
- Parameter index: Int.
- Parameter animated: Bool.
*/
fileprivate func setInternalSelectedIndex(index: Int, animated: Bool = false) {
guard animated else {
internalSelectedIndex = index
return
}
let direction: UIPageViewControllerNavigationDirection = index < internalSelectedIndex ? .reverse : .forward
isTabSelectedAnimation = true isTabSelectedAnimation = true
selectedIndex = index
pageTabBar.select(at: selectedIndex) internalSelectedIndex = index
pageTabBar.select(at: internalSelectedIndex)
setViewControllers([viewControllers[index]], direction: direction, animated: true) { [weak self] _ in setViewControllers([viewControllers[internalSelectedIndex]], direction: direction, animated: true) { [weak self] _ in
guard let s = self else { guard let s = self else {
return return
} }
s.isTabSelectedAnimation = false s.isTabSelectedAnimation = false
s.delegate?.pageTabBarController?(pageTabBarController: s, didTransitionTo: s.viewControllers[s.selectedIndex]) s.delegate?.pageTabBarController?(pageTabBarController: s, didTransitionTo: s.viewControllers[s.internalSelectedIndex])
} }
} }
/// Prepares the pageTabBar.
private func preparePageTabBar() {
pageTabBar.zPosition = 1000
pageTabBar.dividerColor = Color.grey.lighten3
view.addSubview(pageTabBar)
pageTabBar.select(at: selectedIndex)
}
} }
extension PageTabBarController: UIPageViewControllerDelegate { extension PageTabBarController: UIPageViewControllerDelegate {
...@@ -290,8 +319,9 @@ extension PageTabBarController: UIPageViewControllerDelegate { ...@@ -290,8 +319,9 @@ extension PageTabBarController: UIPageViewControllerDelegate {
return return
} }
selectedIndex = index setInternalSelectedIndex(index: index)
pageTabBar.select(at: selectedIndex)
pageTabBar.select(at: index)
if finished && completed { if finished && completed {
delegate?.pageTabBarController?(pageTabBarController: self, didTransitionTo: v) delegate?.pageTabBarController?(pageTabBarController: self, didTransitionTo: v)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment