Commit 59d5aaf4 by Daniel Dahan

development: issue-624: update TabBar to have the same fix as the Switch, where…

development: issue-624: update TabBar to have the same fix as the Switch, where delegates are only triggered from user interaction
parent 371f183a
...@@ -186,7 +186,7 @@ open class Switch: UIControl { ...@@ -186,7 +186,7 @@ open class Switch: UIControl {
return .on == internalSwitchState return .on == internalSwitchState
} }
set(value) { set(value) {
updateSwitchState(state: .on, animated: true, isSwitchStateTriggeredByUserInteraction: false) updateSwitchState(state: .on, animated: true, isTriggeredByUserInteraction: false)
} }
} }
...@@ -335,7 +335,7 @@ open class Switch: UIControl { ...@@ -335,7 +335,7 @@ open class Switch: UIControl {
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
open func toggle(completion: ((Switch) -> Void)? = nil) { open func toggle(completion: ((Switch) -> Void)? = nil) {
updateSwitchState(state: .on == internalSwitchState ? .off : .on, animated: true, isSwitchStateTriggeredByUserInteraction: false, completion: completion) updateSwitchState(state: .on == internalSwitchState ? .off : .on, animated: true, isTriggeredByUserInteraction: false, completion: completion)
} }
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
...@@ -343,7 +343,7 @@ open class Switch: UIControl { ...@@ -343,7 +343,7 @@ open class Switch: UIControl {
return return
} }
updateSwitchState(state: .on == internalSwitchState ? .on : .off, animated: true, isSwitchStateTriggeredByUserInteraction: true) updateSwitchState(state: .on == internalSwitchState ? .on : .off, animated: true, isTriggeredByUserInteraction: true)
} }
/** /**
...@@ -371,18 +371,18 @@ extension Switch { ...@@ -371,18 +371,18 @@ extension Switch {
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
open func setSwitchState(state: SwitchState, animated: Bool = true, completion: ((Switch) -> Void)? = nil) { open func setSwitchState(state: SwitchState, animated: Bool = true, completion: ((Switch) -> Void)? = nil) {
updateSwitchState(state: state, animated: animated, isSwitchStateTriggeredByUserInteraction: false, completion: completion) updateSwitchState(state: state, animated: animated, isTriggeredByUserInteraction: false, completion: completion)
} }
/** /**
Set the switchState property with an option to animate. Set the switchState property with an option to animate.
- Parameter state: The SwitchState to set. - Parameter state: The SwitchState to set.
- Parameter animated: A Boolean indicating to set the animation or not. - Parameter animated: A Boolean indicating to set the animation or not.
- Parameter isSwitchStateTriggeredByUserInteraction: A boolean indicating whether the - Parameter isTriggeredByUserInteraction: A boolean indicating whether the
state was changed by a user interaction, true if yes, false otherwise. state was changed by a user interaction, true if yes, false otherwise.
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
fileprivate func updateSwitchState(state: SwitchState, animated: Bool, isSwitchStateTriggeredByUserInteraction: Bool, completion: ((Switch) -> Void)? = nil) { fileprivate func updateSwitchState(state: SwitchState, animated: Bool, isTriggeredByUserInteraction: Bool, completion: ((Switch) -> Void)? = nil) {
guard isEnabled && internalSwitchState != state else { guard isEnabled && internalSwitchState != state else {
return return
} }
...@@ -391,7 +391,7 @@ extension Switch { ...@@ -391,7 +391,7 @@ extension Switch {
if animated { if animated {
animateToState(state: state) { [weak self] _ in animateToState(state: state) { [weak self] _ in
guard isSwitchStateTriggeredByUserInteraction else { guard isTriggeredByUserInteraction else {
return return
} }
...@@ -407,7 +407,7 @@ extension Switch { ...@@ -407,7 +407,7 @@ extension Switch {
button.x = .on == state ? self.onPosition : self.offPosition button.x = .on == state ? self.onPosition : self.offPosition
styleForState(state: state) styleForState(state: state)
guard isSwitchStateTriggeredByUserInteraction else { guard isTriggeredByUserInteraction else {
return return
} }
...@@ -511,13 +511,13 @@ extension Switch { ...@@ -511,13 +511,13 @@ extension Switch {
} }
let q: CGFloat = sender.x + v.location(in: sender).x - v.previousLocation(in: sender).x let q: CGFloat = sender.x + v.location(in: sender).x - v.previousLocation(in: sender).x
updateSwitchState(state: q > (width - button.width) / 2 ? .on : .off, animated: true, isSwitchStateTriggeredByUserInteraction: true) updateSwitchState(state: q > (width - button.width) / 2 ? .on : .off, animated: true, isTriggeredByUserInteraction: true)
} }
/// Handles the TouchUpInside event. /// Handles the TouchUpInside event.
@objc @objc
fileprivate func handleTouchUpInside() { fileprivate func handleTouchUpInside() {
updateSwitchState(state: isOn ? .off : .on, animated: true, isSwitchStateTriggeredByUserInteraction: true) updateSwitchState(state: isOn ? .off : .on, animated: true, isTriggeredByUserInteraction: true)
} }
/** /**
...@@ -562,7 +562,7 @@ extension Switch { ...@@ -562,7 +562,7 @@ extension Switch {
- Parameter state: The SwitchState to set. - Parameter state: The SwitchState to set.
*/ */
fileprivate func prepareSwitchState(state: SwitchState = .off) { fileprivate func prepareSwitchState(state: SwitchState = .off) {
updateSwitchState(state: state, animated: false, isSwitchStateTriggeredByUserInteraction: false) updateSwitchState(state: state, animated: false, isTriggeredByUserInteraction: false)
} }
/** /**
......
...@@ -156,7 +156,7 @@ open class TabBar: Bar { ...@@ -156,7 +156,7 @@ open class TabBar: Bar {
/// Handles the button touch event. /// Handles the button touch event.
@objc @objc
internal func handleButton(button: UIButton) { internal func handleButton(button: UIButton) {
animate(to: button) animate(to: button, isTriggeredByUserInteraction: true)
} }
/** /**
...@@ -168,30 +168,51 @@ open class TabBar: Bar { ...@@ -168,30 +168,51 @@ open class TabBar: Bar {
guard -1 < index, index < buttons.count else { guard -1 < index, index < buttons.count else {
return return
} }
animate(to: buttons[index], completion: completion) animate(to: buttons[index], isTriggeredByUserInteraction: false, completion: completion)
} }
/** /**
Animates to a given button. Animates to a given button.
- Parameter to button: A UIButton. - Parameter to button: A UIButton.
- Paramater completion: An optional completion block. - Parameter completion: An optional completion block.
*/ */
open func animate(to button: UIButton, completion: ((UIButton) -> Void)? = nil) { open func animate(to button: UIButton, completion: ((UIButton) -> Void)? = nil) {
delegate?.tabBar?(tabBar: self, willSelect: button) animate(to: button, isTriggeredByUserInteraction: false, completion: completion)
}
/**
Animates to a given button.
- Parameter to button: A UIButton.
- Parameter isTriggeredByUserInteraction: A boolean indicating whether the
state was changed by a user interaction, true if yes, false otherwise.
- Parameter completion: An optional completion block.
*/
open func animate(to button: UIButton, isTriggeredByUserInteraction: Bool, completion: ((UIButton) -> Void)? = nil) {
if isTriggeredByUserInteraction {
delegate?.tabBar?(tabBar: self, willSelect: button)
}
selected = button selected = button
isAnimating = true isAnimating = true
UIView.animate(withDuration: 0.25, animations: { [weak self, button = button] in UIView.animate(withDuration: 0.25, animations: { [weak self, button = button] in
guard let s = self else { guard let s = self else {
return return
} }
s.line.center.x = button.center.x s.line.center.x = button.center.x
s.line.width = button.width s.line.width = button.width
}) { [weak self, button = button, completion = completion] _ in }) { [weak self, button = button, completion = completion] _ in
guard let s = self else { guard let s = self else {
return return
} }
s.isAnimating = false s.isAnimating = false
s.delegate?.tabBar?(tabBar: s, didSelect: button)
if isTriggeredByUserInteraction {
s.delegate?.tabBar?(tabBar: s, didSelect: button)
}
completion?(button) completion?(button)
} }
} }
......
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