Commit cdc171a1 by Daniel Dahan

development: added delegation method to PageTabBarController

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