Commit cdc171a1 by Daniel Dahan

development: added delegation method to PageTabBarController

parent 51c63666
...@@ -44,6 +44,7 @@ open class PageTabBar: TabBar { ...@@ -44,6 +44,7 @@ open class PageTabBar: TabBar {
open override func prepareView() { open override func prepareView() {
super.prepareView() super.prepareView()
isLineAnimated = false isLineAnimated = false
lineAlignment = .top
} }
} }
...@@ -88,7 +89,13 @@ extension UIViewController { ...@@ -88,7 +89,13 @@ extension UIViewController {
@objc(PageTabBarControllerDelegate) @objc(PageTabBarControllerDelegate)
public protocol PageTabBarControllerDelegate { public protocol PageTabBarControllerDelegate {
/**
A delegation method that is executed when a UIViewController did transition to.
- Parameter pageTabBarController: A PageTabBarController.
- Parameter willTransitionTo viewController: A UIViewController.
*/
@objc
optional func pageTabBarController(pageTabBarController: PageTabBarController, didTransitionTo viewController: UIViewController)
} }
@objc(PageTabBarController) @objc(PageTabBarController)
...@@ -100,7 +107,7 @@ open class PageTabBarController: RootController { ...@@ -100,7 +107,7 @@ open class PageTabBarController: RootController {
open internal(set) var selectedIndex: Int = 0 open internal(set) var selectedIndex: Int = 0
/// PageTabBar alignment setting. /// PageTabBar alignment setting.
open var pageTabBarAlignment = PageTabBarAlignment.top open var pageTabBarAlignment = PageTabBarAlignment.bottom
/// Reference to the PageTabBar. /// Reference to the PageTabBar.
open internal(set) var pageTabBar: PageTabBar! open internal(set) var pageTabBar: PageTabBar!
...@@ -228,11 +235,14 @@ open class PageTabBarController: RootController { ...@@ -228,11 +235,14 @@ open class PageTabBarController: RootController {
isTabSelectedAnimation = true isTabSelectedAnimation = true
selectedIndex = index selectedIndex = index
pageTabBar.select(at: selectedIndex)
setViewControllers([viewControllers[index]], direction: direction, animated: true) { [weak self] _ in setViewControllers([viewControllers[index]], 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])
} }
} }
...@@ -266,6 +276,10 @@ extension PageTabBarController: UIPageViewControllerDelegate { ...@@ -266,6 +276,10 @@ extension PageTabBarController: UIPageViewControllerDelegate {
selectedIndex = index selectedIndex = index
pageTabBar.select(at: selectedIndex) pageTabBar.select(at: selectedIndex)
if finished && completed {
delegate?.pageTabBarController?(pageTabBarController: self, didTransitionTo: v)
}
} }
} }
...@@ -301,12 +315,11 @@ extension PageTabBarController: UIPageViewControllerDataSource { ...@@ -301,12 +315,11 @@ extension PageTabBarController: UIPageViewControllerDataSource {
extension PageTabBarController: UIScrollViewDelegate { extension PageTabBarController: UIScrollViewDelegate {
public func scrollViewDidScroll(_ scrollView: UIScrollView) { public func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !isTabSelectedAnimation else { guard !pageTabBar.isAnimating else {
pageTabBar.select(at: selectedIndex)
return return
} }
guard 0 < view.width else { guard !isTabSelectedAnimation else {
return return
} }
...@@ -314,8 +327,12 @@ extension PageTabBarController: UIScrollViewDelegate { ...@@ -314,8 +327,12 @@ extension PageTabBarController: UIScrollViewDelegate {
return return
} }
guard 0 < view.width else {
return
}
let x = (scrollView.contentOffset.x - view.width) / scrollView.contentSize.width * view.width let x = (scrollView.contentOffset.x - view.width) / scrollView.contentSize.width * view.width
pageTabBar.line.x = selected.x + x // - (0 < selectedIndex ? pageTabBar.grid.interimSpace * CGFloat(selectedIndex - 1) : 0) pageTabBar.line.center.x = selected.center.x + x
} }
} }
...@@ -58,6 +58,9 @@ public protocol TabBarDelegate { ...@@ -58,6 +58,9 @@ public protocol TabBarDelegate {
} }
open class TabBar: View { open class TabBar: View {
/// A boolean indicating if the TabBar line is in an animation state.
open internal(set) var isAnimating = false
/// Will render the view. /// Will render the view.
open var willRenderView: Bool { open var willRenderView: Bool {
return 0 < width && 0 < height && nil != superview return 0 < width && 0 < height && nil != superview
...@@ -222,17 +225,18 @@ open class TabBar: View { ...@@ -222,17 +225,18 @@ open class TabBar: View {
open func animate(to button: UIButton, completion: (@escaping (UIButton) -> Void)? = nil) { open func animate(to button: UIButton, completion: (@escaping (UIButton) -> Void)? = nil) {
delegate?.tabBarWillSelectButton?(tabBar: self, button: button) delegate?.tabBarWillSelectButton?(tabBar: self, button: button)
selected = button selected = button
isAnimating = true
UIView.animate(withDuration: 0.25, animations: { [weak self, button = button] in UIView.animate(withDuration: 0.25, animations: { [weak self, button = button] in
guard let s = self else { guard let s = self else {
return return
} }
s.line.x = button.x s.line.center.x = button.center.x
s.line.width = button.width s.line.width = button.width
}) { [weak self, button = button, completion = completion] _ in }) { [weak self, button = button, completion = completion] _ in
guard let s = self else { guard let s = self else {
return return
} }
s.isAnimating = false
s.delegate?.tabBarDidSelectButton?(tabBar: s, button: button) s.delegate?.tabBarDidSelectButton?(tabBar: s, button: button)
completion?(button) completion?(button)
} }
...@@ -247,8 +251,7 @@ open class TabBar: View { ...@@ -247,8 +251,7 @@ open class TabBar: View {
*/ */
open override func prepareView() { open override func prepareView() {
super.prepareView() super.prepareView()
interimSpacePreset = .none
contentEdgeInsetsPreset = .none
autoresizingMask = .flexibleWidth autoresizingMask = .flexibleWidth
prepareLine() prepareLine()
prepareDivider() prepareDivider()
...@@ -266,6 +269,7 @@ open class TabBar: View { ...@@ -266,6 +269,7 @@ open class TabBar: View {
/// Prepares the divider. /// Prepares the divider.
private func prepareDivider() { private func prepareDivider() {
divider = Divider(view: self) divider = Divider(view: self)
divider.alignment = .top
} }
/** /**
......
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