Commit e147eb9a by Daniel Dahan

storyboard-examples: updated Switch and fixed an issue where Switch in a Bar or…

storyboard-examples: updated Switch and fixed an issue where Switch in a Bar or NavigationBar would behave incorrectly, issue-540
parent 1fc740e4
......@@ -36,6 +36,7 @@ class RootViewController: UIViewController {
private var menuButton: IconButton!
private var starButton: IconButton!
private var searchButton: IconButton!
private var switchControl: Switch!
/// Trigger to go to the next view controller.
private var nextButton: FlatButton!
......@@ -77,7 +78,9 @@ class RootViewController: UIViewController {
navigationItem.detail = "Build Beautiful Software"
navigationItem.detailLabel.textColor = Color.lightBlue.lighten5
navigationItem.leftViews = [menuButton]
switchControl = Switch()
navigationItem.leftViews = [switchControl, menuButton]
navigationItem.rightViews = [starButton, searchButton]
}
......
......@@ -36,7 +36,6 @@ class RootViewController: UIViewController {
private var menuButton: IconButton!
private var starButton: IconButton!
private var searchButton: IconButton!
private var switchControl: Switch!
open override func viewDidLoad() {
super.viewDidLoad()
......@@ -75,10 +74,8 @@ class RootViewController: UIViewController {
tc.toolbar.detail = "Build Beautiful Software"
tc.toolbar.detailLabel.textAlignment = .left
switchControl = Switch(state: .off, style:.light, size: .small)
tc.toolbar.leftViews = [menuButton]
tc.toolbar.rightViews = [switchControl, starButton, searchButton]
tc.toolbar.rightViews = [starButton, searchButton]
}
}
......@@ -191,6 +191,7 @@ open class Bar: View {
for v in leftViews {
(v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......@@ -203,6 +204,7 @@ open class Bar: View {
for v in rightViews {
(v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......
......@@ -209,6 +209,7 @@ open class NavigationBar: UINavigationBar {
for v in item.leftViews {
(v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......@@ -221,6 +222,7 @@ open class NavigationBar: UINavigationBar {
for v in item.rightViews {
(v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......
......@@ -238,21 +238,11 @@ open class Switch: UIControl {
trackThickness = 24
buttonDiameter = 32
}
frame.size = intrinsicContentSize
}
}
open override var frame: CGRect {
didSet {
reload()
}
}
open override var bounds: CGRect {
didSet {
reload()
}
}
open override var intrinsicContentSize: CGSize {
switch switchSize {
case .small:
......@@ -299,12 +289,17 @@ open class Switch: UIControl {
track = UIView()
button = FabButton()
super.init(frame: .zero)
prepare()
prepareSwitchSize(size: size)
prepareSwitchStyle(style: style)
prepare()
prepareSwitchState(state: state)
prepareSwitchStyle(style: style)
prepareSwitchSize(size: size)
}
open override func layoutSubviews() {
super.layoutSubviews()
reload()
}
open override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
styleForState(state: internalSwitchState)
......@@ -339,13 +334,16 @@ open class Switch: UIControl {
}
internalSwitchState = state
if animated {
animateToState(state: state) { [weak self] _ in
if let s: Switch = self {
s.sendActions(for: .valueChanged)
completion?(s)
s.delegate?.switchDidChangeState(control: s, state: s.internalSwitchState)
guard let s = self else {
return
}
s.sendActions(for: .valueChanged)
completion?(s)
s.delegate?.switchDidChangeState(control: s, state: s.internalSwitchState)
}
} else {
button.x = .on == state ? self.onPosition : self.offPosition
......@@ -415,11 +413,12 @@ open class Switch: UIControl {
when subclassing.
*/
open func prepare() {
contentScaleFactor = Device.scale
prepareTrack()
prepareButton()
prepareSwitchSize(size: .medium)
prepareSwitchStyle(style: .light)
prepareSwitchState(state: .off)
prepareSwitchState()
prepareSwitchStyle()
prepareSwitchSize()
}
/// Laout the button and track views.
......@@ -459,7 +458,7 @@ open class Switch: UIControl {
init to set the state value and have an effect.
- Parameter state: The SwitchState to set.
*/
private func prepareSwitchState(state: SwitchState) {
private func prepareSwitchState(state: SwitchState = .off) {
setSwitchState(state: state, animated: false)
}
......@@ -468,7 +467,7 @@ open class Switch: UIControl {
init to set the state value and have an effect.
- Parameter style: The SwitchStyle to set.
*/
private func prepareSwitchStyle(style: SwitchStyle) {
private func prepareSwitchStyle(style: SwitchStyle = .light) {
switchStyle = style
}
......@@ -477,7 +476,7 @@ open class Switch: UIControl {
init to set the size value and have an effect.
- Parameter size: The SwitchSize to set.
*/
private func prepareSwitchSize(size: SwitchSize) {
private func prepareSwitchSize(size: SwitchSize = .medium) {
switchSize = size
}
......@@ -532,21 +531,27 @@ open class Switch: UIControl {
delay: 0.05,
options: [.curveEaseIn, .curveEaseOut],
animations: { [weak self] in
if let s: Switch = self {
s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset
s.styleForState(state: state)
}
guard let s = self else {
return
}
s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset
s.styleForState(state: state)
}) { [weak self] _ in
UIView.animate(withDuration: 0.15,
animations: { [weak self] in
if let s: Switch = self {
s.button.x = .on == state ? s.onPosition : s.offPosition
}
guard let s = self else {
return
}
s.button.x = .on == state ? s.onPosition : s.offPosition
}) { [weak self] _ in
if let s: Switch = self {
s.isUserInteractionEnabled = true
completion?(s)
}
guard let s = self else {
return
}
s.isUserInteractionEnabled = true
completion?(s)
}
}
}
......
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