From a58facf2d7eceed629c02dc49b53e860c87414c1 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 18 Aug 2022 09:38:59 -0500 Subject: [PATCH] udpated toggle Signed-off-by: Matt Bruce --- VDS/Components/Toggle/Toggle.swift | 52 ++++++------------------------ 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/VDS/Components/Toggle/Toggle.swift b/VDS/Components/Toggle/Toggle.swift index 0272fe67..e23d6fff 100644 --- a/VDS/Components/Toggle/Toggle.swift +++ b/VDS/Components/Toggle/Toggle.swift @@ -258,6 +258,7 @@ open class ToggleBase: Control, Changable { //-------------------------------------------------- open override func setup() { super.setup() + addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(Self.tap))) isAccessibilityElement = true accessibilityTraits = .button @@ -317,21 +318,24 @@ open class ToggleBase: Control, Changable { //-------------------------------------------------- // MARK: - Actions //-------------------------------------------------- - open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { - super.sendAction(action, to: target, for: event) - toggleAndAction() - } - open override func sendActions(for controlEvents: UIControl.Event) { super.sendActions(for: controlEvents) - toggleAndAction() + if controlEvents.contains(.touchUpInside) { + toggleAndAction() + } } /// This will toggle the state of the Toggle and execute the actionBlock if provided. public func toggleAndAction() { isOn.toggle() + sendActions(for: .valueChanged) onChange?() } + + @objc func tap() { + sendActions(for: .touchUpInside) + + } override open func accessibilityActivate() -> Bool { // Hold state in case User wanted isAnimated to remain off. @@ -339,42 +343,6 @@ open class ToggleBase: Control, Changable { sendActions(for: .touchUpInside) return true } - - //-------------------------------------------------- - // MARK: - UIResponder - //-------------------------------------------------- - open override func touchesBegan(_ touches: Set, with event: UIEvent?) { - - UIView.animate(withDuration: 0.1, animations: { - self.knobWidthConstraint?.constant += Constants.PaddingOne - self.layoutIfNeeded() - }) - } - - public override func touchesEnded(_ touches: Set, with event: UIEvent?) { - knobReformAnimation() - // Action only occurs of the user lifts up from withing acceptable region of the toggle. - let value = 20.0 - guard let coordinates = touches.first?.location(in: self) else { return } - guard coordinates.x > -value else { return } - - sendActions(for: .touchUpInside) - } - - open override func touchesCancelled(_ touches: Set, with event: UIEvent?) { - knobReformAnimation() - sendActions(for: .touchCancel) - } - - //-------------------------------------------------- - // MARK: - Animations - //-------------------------------------------------- - public func knobReformAnimation() { - UIView.animate(withDuration: 0.1, animations: { - self.knobWidthConstraint?.constant = self.knobSize.width - self.layoutIfNeeded() - }, completion: nil) - } //-------------------------------------------------- // MARK: - State