Comments, logical updates, refactoring.

This commit is contained in:
Kevin G Christiano 2019-12-11 15:58:18 -05:00
parent eab910011e
commit 98997bfa80

View File

@ -14,6 +14,8 @@ public typealias ActionBlockConfirmation = () -> (Bool)
/**
A custom implementation of Apple's UISwitch.
By default this class begins in the off state.
Container: The background of the toggle control.
Knob: The circular indicator that slides on the container.
*/
@ -29,6 +31,8 @@ public typealias ActionBlockConfirmation = () -> (Bool)
public var isAnimated = true
public var didToggleAction: ActionBlock?
/// Executes logic before state change. If false, then toggle state will not change and the didToggleAction will not execute.
public var shouldToggleAction: ActionBlockConfirmation? = {
return { return true }
}()
@ -51,8 +55,9 @@ public typealias ActionBlockConfirmation = () -> (Bool)
open override var isEnabled: Bool {
didSet {
isUserInteractionEnabled = isEnabled
backgroundColor = isEnabled ? containerTintColor?.on : disabledTintColor?.container
knobView.backgroundColor = isEnabled ? knobTintColor?.on : disabledTintColor?.knob
changeStateNoAnimation(isEnabled)
backgroundColor = isEnabled ? containerTintColor?.off : disabledTintColor?.container
knobView.backgroundColor = isEnabled ? knobTintColor?.off : disabledTintColor?.knob
}
}
@ -116,8 +121,8 @@ public typealias ActionBlockConfirmation = () -> (Bool)
private func constrainKnob() {
self.knobLeadingConstraint?.isActive = !isOn
self.knobTrailingConstraint?.isActive = isOn
knobLeadingConstraint?.isActive = !isOn
knobTrailingConstraint?.isActive = isOn
}
//--------------------------------------------------
@ -133,17 +138,21 @@ public typealias ActionBlockConfirmation = () -> (Bool)
self.init(frame: .zero)
}
public convenience init(isOn: Bool, didToggleAction: ActionBlock?) {
public convenience init(isOn: Bool) {
self.init(frame: .zero)
self.isOn = isOn
self.didToggleAction = didToggleAction
}
public convenience init(didToggleAction: ActionBlock?) {
/// - parameter isOn: Bool to set the state of the toggle.
/// - parameter didToggleAction: A closure which is executed after the toggle changes states.
public convenience init(isOn: Bool = false, didToggleAction: ActionBlock?) {
self.init(frame: .zero)
changeStateNoAnimation(isOn)
self.didToggleAction = didToggleAction
}
/// - parameter shouldToggleAction: Takes a closure that returns a boolean.
/// - parameter didToggleAction: A closure which is executed after the toggle changes states.
public convenience init(shouldToggleAction: ActionBlockConfirmation?, didToggleAction: ActionBlock?) {
self.init(frame: .zero)
self.didToggleAction = didToggleAction
@ -246,6 +255,13 @@ public typealias ActionBlockConfirmation = () -> (Bool)
}
}
private func changeStateNoAnimation(_ state: Bool) {
isAnimated = false
isOn = state
isAnimated = true
}
//--------------------------------------------------
// MARK: - UIResponder
//--------------------------------------------------
@ -357,9 +373,7 @@ extension Toggle {
}
if let state = dictionary["state"] as? Bool {
isAnimated = false
isOn = state
isAnimated = true
changeStateNoAnimation(state)
}
if let actionMap = dictionary.optionalDictionaryForKey("actionMap") {