Commit 8b0aa85d by Daniel Dahan

development: added selected property for TabBar that identifies which button is currently selected

parent 15e00b24
...@@ -37,37 +37,7 @@ public enum TabBarLineAlignment: Int { ...@@ -37,37 +37,7 @@ public enum TabBarLineAlignment: Int {
} }
open class TabBar: View { open class TabBar: View {
/// A reference to the line UIView. /// Will render the view.
internal var line: UIView!
/// The line color.
open var lineColor: UIColor? {
get {
return line.backgroundColor
}
set(value) {
line.backgroundColor = value
}
}
/// A value for the line alignment.
open var lineAlignment = TabBarLineAlignment.bottom {
didSet {
layoutSubviews()
}
}
/// The line height.
open var lineHeight: CGFloat {
get {
return line.height
}
set(value) {
line.height = value
}
}
/// 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
} }
...@@ -115,6 +85,9 @@ open class TabBar: View { ...@@ -115,6 +85,9 @@ open class TabBar: View {
return CGSize(width: width, height: 49) return CGSize(width: width, height: 49)
} }
/// The currently selected button.
open var selected: UIButton?
/// Buttons. /// Buttons.
open var buttons = [UIButton]() { open var buttons = [UIButton]() {
didSet { didSet {
...@@ -128,6 +101,36 @@ open class TabBar: View { ...@@ -128,6 +101,36 @@ open class TabBar: View {
} }
} }
/// A reference to the line UIView.
internal var line: UIView!
/// The line color.
open var lineColor: UIColor? {
get {
return line.backgroundColor
}
set(value) {
line.backgroundColor = value
}
}
/// A value for the line alignment.
open var lineAlignment = TabBarLineAlignment.bottom {
didSet {
layoutSubviews()
}
}
/// The line height.
open var lineHeight: CGFloat {
get {
return line.height
}
set(value) {
line.height = value
}
}
/// Layer Reference. /// Layer Reference.
open internal(set) var divider: Divider! open internal(set) var divider: Divider!
...@@ -144,7 +147,12 @@ open class TabBar: View { ...@@ -144,7 +147,12 @@ open class TabBar: View {
b.addTarget(self, action: #selector(handleButton(button:)), for: .touchUpInside) b.addTarget(self, action: #selector(handleButton(button:)), for: .touchUpInside)
} }
grid.reload() grid.reload()
line.frame = CGRect(x: 0, y: .bottom == lineAlignment ? height - lineHeight : 0, width: buttons.first!.width, height: lineHeight)
if nil == selected {
selected = buttons.first
}
line.frame = CGRect(x: selected!.x, y: .bottom == lineAlignment ? height - lineHeight : 0, width: selected!.width, height: lineHeight)
} }
divider.reload() divider.reload()
} }
...@@ -153,7 +161,9 @@ open class TabBar: View { ...@@ -153,7 +161,9 @@ open class TabBar: View {
/// Handles the button touch event. /// Handles the button touch event.
@objc @objc
internal func handleButton(button: UIButton) { internal func handleButton(button: UIButton) {
UIView.animate(withDuration: 0.25, animations: { [weak self] in selected = button
UIView.animate(withDuration: 0.25, animations: { [weak self] in
if let s = self { if let s = self {
s.line.x = button.x s.line.x = button.x
s.line.width = button.width s.line.width = button.width
......
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