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() {
super.setup()
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(Self.tap)))
isAccessibilityElement = true
accessibilityTraits = .button
@ -317,21 +318,24 @@ open class ToggleBase<ModelType: ToggleModel>: Control<ModelType>, 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<ModelType: ToggleModel>: Control<ModelType>, Changable {
sendActions(for: .touchUpInside)
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