udpated toggle

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-18 09:38:59 -05:00
parent 8fd2c833f0
commit a58facf2d7

View File

@ -258,6 +258,7 @@ open class ToggleBase<ModelType: ToggleModel>: Control<ModelType>, Changable {
//-------------------------------------------------- //--------------------------------------------------
open override func setup() { open override func setup() {
super.setup() super.setup()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(Self.tap)))
isAccessibilityElement = true isAccessibilityElement = true
accessibilityTraits = .button accessibilityTraits = .button
@ -317,22 +318,25 @@ open class ToggleBase<ModelType: ToggleModel>: Control<ModelType>, Changable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Actions // 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) { open override func sendActions(for controlEvents: UIControl.Event) {
super.sendActions(for: controlEvents) super.sendActions(for: controlEvents)
toggleAndAction() if controlEvents.contains(.touchUpInside) {
toggleAndAction()
}
} }
/// This will toggle the state of the Toggle and execute the actionBlock if provided. /// This will toggle the state of the Toggle and execute the actionBlock if provided.
public func toggleAndAction() { public func toggleAndAction() {
isOn.toggle() isOn.toggle()
sendActions(for: .valueChanged)
onChange?() onChange?()
} }
@objc func tap() {
sendActions(for: .touchUpInside)
}
override open func accessibilityActivate() -> Bool { override open func accessibilityActivate() -> Bool {
// Hold state in case User wanted isAnimated to remain off. // Hold state in case User wanted isAnimated to remain off.
guard isUserInteractionEnabled else { return false } guard isUserInteractionEnabled else { return false }
@ -340,42 +344,6 @@ open class ToggleBase<ModelType: ToggleModel>: Control<ModelType>, Changable {
return true return true
} }
//--------------------------------------------------
// MARK: - UIResponder
//--------------------------------------------------
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.1, animations: {
self.knobWidthConstraint?.constant += Constants.PaddingOne
self.layoutIfNeeded()
})
}
public override func touchesEnded(_ touches: Set<UITouch>, 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<UITouch>, 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 // MARK: - State
//-------------------------------------------------- //--------------------------------------------------