Commit beb00b83 by Daniel Dahan

issue-860: Added TabBar color states

parent 543012fd
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'Material' s.name = 'Material'
s.version = '2.11.4' s.version = '2.12.0'
s.license = 'BSD-3-Clause' s.license = 'BSD-3-Clause'
s.summary = 'A UI/UX framework for creating beautiful applications.' s.summary = 'A UI/UX framework for creating beautiful applications.'
s.homepage = 'http://materialswift.com' s.homepage = 'http://materialswift.com'
......
...@@ -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.11.4</string> <string>2.12.0</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -37,6 +37,12 @@ open class TabItem: FlatButton { ...@@ -37,6 +37,12 @@ open class TabItem: FlatButton {
} }
} }
@objc(TabItemState)
public enum TabItemState: Int {
case normal
case selected
}
@objc(TabBarLineAlignment) @objc(TabBarLineAlignment)
public enum TabBarLineAlignment: Int { public enum TabBarLineAlignment: Int {
case top case top
...@@ -114,6 +120,9 @@ open class TabBar: Bar { ...@@ -114,6 +120,9 @@ open class TabBar: Bar {
return w return w
} }
/// A dictionary of TabItemStates to UIColors.
fileprivate var tabItemsColorForState = [TabItemState: UIColor]()
/// An enum that determines the tab bar style. /// An enum that determines the tab bar style.
open var tabBarStyle = TabBarStyle.auto { open var tabBarStyle = TabBarStyle.auto {
didSet { didSet {
...@@ -198,30 +207,11 @@ open class TabBar: Bar { ...@@ -198,30 +207,11 @@ open class TabBar: Bar {
} }
prepareTabItems() prepareTabItems()
layoutSubviews() layoutSubviews()
} }
} }
/// TabBar items normal title color.
open var tabItemsNormalTitleColor: UIColor? {
didSet {
let normalColor = tabItemsNormalTitleColor ?? Color.blue.base
for v in tabItems {
if (nil == v.titleColor || Color.blue.base == v.titleColor) { v.titleColor = normalColor }
}
}
}
/// TabBar items selected title color.
open var tabItemsSelectedTitleColor: UIColor? {
didSet {
let selectedColor = tabItemsSelectedTitleColor ?? Color.blue.base
for v in tabItems {
if (nil == v.selectedTitleColor || Color.blue.base == v.selectedTitleColor) { v.selectedTitleColor = selectedColor }
}
}
}
/// A reference to the line UIView. /// A reference to the line UIView.
open let line = UIView() open let line = UIView()
...@@ -274,6 +264,9 @@ open class TabBar: Bar { ...@@ -274,6 +264,9 @@ open class TabBar: Bar {
prepareScrollView() prepareScrollView()
prepareDivider() prepareDivider()
prepareLine() prepareLine()
prepareColors()
updateColors()
} }
} }
...@@ -281,7 +274,6 @@ fileprivate extension TabBar { ...@@ -281,7 +274,6 @@ fileprivate extension TabBar {
// Prepares the line. // Prepares the line.
func prepareLine() { func prepareLine() {
line.layer.zPosition = 10000 line.layer.zPosition = 10000
lineColor = Color.blue.base
lineHeight = 3 lineHeight = 3
scrollView.addSubview(line) scrollView.addSubview(line)
} }
...@@ -295,26 +287,29 @@ fileprivate extension TabBar { ...@@ -295,26 +287,29 @@ fileprivate extension TabBar {
/// Prepares the tabItems. /// Prepares the tabItems.
func prepareTabItems() { func prepareTabItems() {
shouldNotAnimateLineView = true shouldNotAnimateLineView = true
let normalColor = tabItemsNormalTitleColor ?? Color.blue.base
let selectedColor = tabItemsSelectedTitleColor ?? Color.blue.base
for v in tabItems { for v in tabItems {
v.grid.columns = 0 v.grid.columns = 0
v.contentEdgeInsets = .zero v.contentEdgeInsets = .zero
if Color.blue.base == v.titleColor { v.titleColor = normalColor }
if nil == v.selectedTitleColor { v.selectedTitleColor = selectedColor } prepareTabItemHandler(tabItem: v)
prepareLineAnimationHandler(tabItem: v)
} }
selectedTabItem = tabItems.first selectedTabItem = tabItems.first
} }
/// Prepares the tabsItems colors.
func prepareColors() {
tabItemsColorForState[.normal] = Color.blue.base
tabItemsColorForState[.selected] = Color.blue.base
}
/** /**
Prepares the line animation handlers. Prepares the line animation handlers.
- Parameter tabItem: A TabItem. - Parameter tabItem: A TabItem.
*/ */
func prepareLineAnimationHandler(tabItem: TabItem) { func prepareTabItemHandler(tabItem: TabItem) {
removeLineAnimationHandler(tabItem: tabItem) removeTabItemHandler(tabItem: tabItem)
tabItem.addTarget(self, action: #selector(handleTabItemsChange(tabItem:)), for: .touchUpInside)
tabItem.addTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside) tabItem.addTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside)
} }
...@@ -394,12 +389,18 @@ fileprivate extension TabBar { ...@@ -394,12 +389,18 @@ fileprivate extension TabBar {
Removes the line animation handlers. Removes the line animation handlers.
- Parameter tabItem: A TabItem. - Parameter tabItem: A TabItem.
*/ */
func removeLineAnimationHandler(tabItem: TabItem) { func removeTabItemHandler(tabItem: TabItem) {
tabItem.removeTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside) tabItem.removeTarget(self, action: #selector(handleLineAnimation(tabItem:)), for: .touchUpInside)
} }
} }
fileprivate extension TabBar { fileprivate extension TabBar {
@objc
func handleTabItemsChange(tabItem: TabItem) {
selectedTabItem = tabItem
updateColors()
}
/// Handles the tabItem touch event. /// Handles the tabItem touch event.
@objc @objc
func handleLineAnimation(tabItem: TabItem) { func handleLineAnimation(tabItem: TabItem) {
...@@ -435,6 +436,40 @@ extension TabBar { ...@@ -435,6 +436,40 @@ extension TabBar {
} }
} }
extension TabBar {
/**
Sets the colors of the tabItems for the state given.
- Parameter _ color: A UIColor.
- Parameter for state: A TabItemState.
*/
open func setColor(_ color: UIColor, for state: TabItemState) {
tabItemsColorForState[state] = color
updateColors()
}
}
fileprivate extension TabBar {
/// Updates the tabItems colors.
func updateColors() {
var color = tabItemsColorForState[.normal]
for v in tabItems {
v.setTitleColor(color, for: .normal)
v.tintColor = color
}
guard let v = selectedTabItem else {
return
}
color = tabItemsColorForState[.selected]
v.setTitleColor(color, for: .selected)
v.tintColor = color
lineColor = color
}
}
fileprivate extension TabBar { fileprivate extension TabBar {
/** /**
Animates to a given tabItem. Animates to a given tabItem.
...@@ -449,8 +484,6 @@ fileprivate extension TabBar { ...@@ -449,8 +484,6 @@ fileprivate extension TabBar {
delegate?.tabBar?(tabBar: self, willSelect: tabItem) delegate?.tabBar?(tabBar: self, willSelect: tabItem)
} }
selectedTabItem = tabItem
line.animate(.duration(0.25), line.animate(.duration(0.25),
.size(width: tabItem.bounds.width, height: lineHeight), .size(width: tabItem.bounds.width, height: lineHeight),
.position(x: tabItem.center.x, y: .bottom == lineAlignment ? bounds.height - lineHeight / 2 : lineHeight / 2), .position(x: tabItem.center.x, y: .bottom == lineAlignment ? bounds.height - lineHeight / 2 : lineHeight / 2),
......
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