Commit 4ca95ebc by Daniel Dahan

adjusting line animation on TabBar changes

parent 31330fc6
...@@ -78,14 +78,21 @@ open class TabBar: Bar { ...@@ -78,14 +78,21 @@ open class TabBar: Bar {
} }
/// An enum that determines the tab bar style. /// An enum that determines the tab bar style.
open var tabBarStyle = TabBarStyle.normal { open var tabBarStyle = TabBarStyle.scrollable {
didSet { didSet {
layoutSubviews() layoutSubviews()
} }
} }
/// A reference to the scroll view when the tab bar style is scrollable. /// A reference to the scroll view when the tab bar style is scrollable.
open fileprivate(set) var scrollView: UIScrollView! open let scrollView = UIScrollView()
/// Does the scroll view bounce.
open var isScrollBounceEnabled = true {
didSet {
scrollView.bounces = true
}
}
/// A delegation reference. /// A delegation reference.
open weak var delegate: TabBarDelegate? open weak var delegate: TabBarDelegate?
...@@ -270,11 +277,7 @@ open class TabBar: Bar { ...@@ -270,11 +277,7 @@ open class TabBar: Bar {
layoutDivider() layoutDivider()
let buttonsWidth = buttons.reduce(0) { if .scrollable == tabBarStyle {
$0 + $1.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: contentView.height)).width + interimSpace
}
if .scrollable == tabBarStyle && buttonsWidth > p {
scrollView.frame = CGRect(x: l, y: 0, width: p, height: height) scrollView.frame = CGRect(x: l, y: 0, width: p, height: height)
var w: CGFloat = 0 var w: CGFloat = 0
...@@ -288,14 +291,14 @@ open class TabBar: Bar { ...@@ -288,14 +291,14 @@ open class TabBar: Bar {
w += width w += width
} }
scrollView.contentSize = CGSize(width: buttonsWidth, height: height) scrollView.contentSize = CGSize(width: w, height: height)
scrollView.addSubview(line)
} else { } else {
contentView.grid.axis.columns = buttons.count contentView.grid.axis.columns = buttons.count
centerViews = buttons centerViews = buttons
addSubview(line)
} }
addSubview(line)
updateSelectionLine() updateSelectionLine()
} }
...@@ -354,8 +357,6 @@ extension TabBar { ...@@ -354,8 +357,6 @@ extension TabBar {
/// Prepares the scroll view. /// Prepares the scroll view.
fileprivate func prepareScrollView() { fileprivate func prepareScrollView() {
scrollView = UIScrollView()
scrollView.bounces = false
scrollView.isPagingEnabled = false scrollView.isPagingEnabled = false
scrollView.showsVerticalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false
......
...@@ -32,12 +32,7 @@ import UIKit ...@@ -32,12 +32,7 @@ import UIKit
fileprivate var TabItemKey: UInt8 = 0 fileprivate var TabItemKey: UInt8 = 0
open class TabItem: FlatButton { open class TabItem: FlatButton {}
open override func prepare() {
super.prepare()
pulseAnimation = .none
}
}
@objc(TabBarAlignment) @objc(TabBarAlignment)
public enum TabBarAlignment: Int { public enum TabBarAlignment: Int {
...@@ -95,6 +90,7 @@ open class TabsController: UIViewController { ...@@ -95,6 +90,7 @@ open class TabsController: UIViewController {
prepareTabBar() prepareTabBar()
prepareContainer() prepareContainer()
prepareViewControllers()
layoutSubviews() layoutSubviews()
} }
} }
...@@ -111,7 +107,7 @@ open class TabsController: UIViewController { ...@@ -111,7 +107,7 @@ open class TabsController: UIViewController {
} }
/// The transition type used during a transition. /// The transition type used during a transition.
open var transitionType = MotionTransitionType.autoReverse(presenting: .fade) open var motionTransitionType = MotionTransitionType.fade
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
...@@ -150,8 +146,7 @@ open class TabsController: UIViewController { ...@@ -150,8 +146,7 @@ open class TabsController: UIViewController {
open func layoutSubviews() { open func layoutSubviews() {
layoutTabBar() layoutTabBar()
layoutContainer() layoutContainer()
layoutViewController(at: selectedIndex)
viewControllers[selectedIndex].view.frame.size = container.bounds.size
} }
/** /**
...@@ -165,10 +160,7 @@ open class TabsController: UIViewController { ...@@ -165,10 +160,7 @@ open class TabsController: UIViewController {
view.contentScaleFactor = Screen.scale view.contentScaleFactor = Screen.scale
prepareContainer() prepareContainer()
prepareTabBar() prepareTabBar()
prepareViewControllers()
for i in 0..<viewControllers.count {
prepareViewController(at: i)
}
} }
} }
...@@ -224,6 +216,21 @@ extension TabsController { ...@@ -224,6 +216,21 @@ extension TabsController {
view.addSubview(container) view.addSubview(container)
} }
/// Prepares all the view controllers.
fileprivate func prepareViewControllers() {
let n = viewControllers.count
for i in 0..<n {
guard i != selectedIndex else {
continue
}
prepareViewController(at: i)
}
prepareViewController(at: selectedIndex)
}
/** /**
Loads a view controller based on its index in the viewControllers Array Loads a view controller based on its index in the viewControllers Array
and adds it as a child view controller. and adds it as a child view controller.
...@@ -267,7 +274,7 @@ extension TabsController { ...@@ -267,7 +274,7 @@ extension TabsController {
container.height = view.height container.height = view.height
} }
container.width = view.bounds.width container.width = view.width
} }
/// Layout the TabBar. /// Layout the TabBar.
...@@ -291,6 +298,11 @@ extension TabsController { ...@@ -291,6 +298,11 @@ extension TabsController {
v.isHidden = true v.isHidden = true
} }
} }
/// Layout the view controller at the given index.
fileprivate func layoutViewController(at index: Int) {
viewControllers[index].view.frame.size = container.bounds.size
}
} }
extension TabsController { extension TabsController {
...@@ -343,10 +355,12 @@ extension TabsController { ...@@ -343,10 +355,12 @@ extension TabsController {
let tvc = viewControllers[i] let tvc = viewControllers[i]
tvc.view.frame.size = container.bounds.size tvc.view.frame.size = container.bounds.size
tvc.motionModalTransitionType = transitionType tvc.motionModalTransitionType = motionTransitionType
Motion.shared.transition(from: fvc, to: tvc, in: container) Motion.shared.transition(from: fvc, to: tvc, in: container)
selectedIndex = i selectedIndex = i
tabBar?.select(at: selectedIndex)
} }
} }
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