411 lines
14 KiB
Swift
411 lines
14 KiB
Swift
//
|
|
// Toggle.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 7/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
/**
|
|
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.
|
|
*/
|
|
@objcMembers open class VDSToggle: Control<VDSToggleModel>, Changable {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
/// Holds the on and off colors for the container.
|
|
public var containerTintColor: (on: UIColor, off: UIColor) = (on: VDSColor.paletteGreen26, off: VDSColor.paletteGray44)
|
|
|
|
/// Holds the on and off colors for the knob.
|
|
public var knobTintColor: (on: UIColor, off: UIColor) = (on: VDSColor.paletteWhite, off: VDSColor.paletteWhite)
|
|
|
|
/// Holds the on and off colors for the disabled state..
|
|
public var disabledTintColor: (container: UIColor, knob: UIColor) = (container: VDSColor.paletteGray11, knob: VDSColor.paletteWhite)
|
|
|
|
/// Set this flag to false if you do not want to animate state changes.
|
|
public var isAnimated = true
|
|
|
|
public var onChange: Blocks.ActionBlock?
|
|
|
|
private var showText: Bool {
|
|
return model?.showText ?? false
|
|
}
|
|
|
|
private var onText: String {
|
|
return model?.onText ?? "On"
|
|
}
|
|
|
|
private var offText: String {
|
|
return model?.offText ?? "off"
|
|
}
|
|
|
|
private var showTextSpacing: CGFloat {
|
|
showText ? 12 : 0
|
|
}
|
|
|
|
private var textPosition: VDSTextPosition {
|
|
return model?.textPosition ?? .left
|
|
}
|
|
|
|
private var fontSize: VDSFontSize {
|
|
return model?.fontSize ?? .small
|
|
}
|
|
|
|
private var fontWeight: VDSFontWeight {
|
|
return model?.fontWeight ?? .regular
|
|
}
|
|
|
|
// Sizes are from InVision design specs.
|
|
public static var toggleSize = CGSize(width: 52, height: 24)
|
|
open class func getToggleScaledSize() -> CGSize { return Self.toggleSize }
|
|
|
|
public static var knobSize = CGSize(width: 20, height: 20)
|
|
open class func getKnobScaledSize() -> CGSize { return Self.knobSize }
|
|
|
|
private var stackView: UIStackView = {
|
|
let stackView = UIStackView()
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
stackView.axis = .horizontal
|
|
stackView.distribution = .fillProportionally
|
|
return stackView
|
|
}()
|
|
|
|
private var label: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
return label
|
|
}()
|
|
|
|
private var toggleView: UIView = {
|
|
let view = UIView()
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
return view
|
|
}()
|
|
|
|
private var knobView: UIView = {
|
|
let view = UIView()
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
view.backgroundColor = .white
|
|
return view
|
|
}()
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Computed Properties
|
|
//--------------------------------------------------
|
|
|
|
open override var isEnabled: Bool {
|
|
didSet {
|
|
isUserInteractionEnabled = isEnabled
|
|
changeStateNoAnimation(isEnabled ? isOn : false)
|
|
setToggleAppearanceFromState()
|
|
setAccessibilityHint(isEnabled)
|
|
}
|
|
}
|
|
|
|
/// Simple means to prevent user interaction with the toggle.
|
|
public var isLocked: Bool = false {
|
|
didSet { isUserInteractionEnabled = !isLocked }
|
|
}
|
|
|
|
/// The state on the toggle. Default value: false.
|
|
open var isOn: Bool = false {
|
|
didSet {
|
|
label.text = isOn ? onText : offText
|
|
|
|
if isAnimated {
|
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
|
|
if self.isOn {
|
|
self.knobView.backgroundColor = self.knobTintColor.on
|
|
self.toggleView.backgroundColor = self.containerTintColor.on
|
|
|
|
} else {
|
|
self.knobView.backgroundColor = self.knobTintColor.off
|
|
self.toggleView.backgroundColor = self.containerTintColor.off
|
|
}
|
|
}, completion: nil)
|
|
|
|
UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.2, options: [], animations: {
|
|
self.constrainKnob()
|
|
self.knobWidthConstraint?.constant = Self.getKnobScaledSize().width
|
|
self.layoutIfNeeded()
|
|
}, completion: nil)
|
|
|
|
} else {
|
|
setToggleAppearanceFromState()
|
|
self.constrainKnob()
|
|
}
|
|
|
|
model?.on = isOn
|
|
setAccessibilityValue(isOn)
|
|
setNeedsLayout()
|
|
layoutIfNeeded()
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Constraints
|
|
//--------------------------------------------------
|
|
|
|
private var knobLeadingConstraint: NSLayoutConstraint?
|
|
private var knobTrailingConstraint: NSLayoutConstraint?
|
|
private var knobHeightConstraint: NSLayoutConstraint?
|
|
private var knobWidthConstraint: NSLayoutConstraint?
|
|
private var toggleHeightConstraint: NSLayoutConstraint?
|
|
private var toggleWidthConstraint: NSLayoutConstraint?
|
|
|
|
private func constrainKnob() {
|
|
|
|
knobLeadingConstraint?.isActive = false
|
|
knobTrailingConstraint?.isActive = false
|
|
|
|
_ = isOn ? constrainKnobOn() : constrainKnobOff()
|
|
|
|
knobTrailingConstraint?.isActive = true
|
|
knobLeadingConstraint?.isActive = true
|
|
}
|
|
|
|
private func constrainKnobOn() {
|
|
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(equalTo: knobView.trailingAnchor, constant: 2)
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(greaterThanOrEqualTo: toggleView.leadingAnchor)
|
|
}
|
|
|
|
private func constrainKnobOff() {
|
|
|
|
knobTrailingConstraint = toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: knobView.trailingAnchor)
|
|
knobLeadingConstraint = knobView.leadingAnchor.constraint(equalTo: toggleView.leadingAnchor, constant: 2)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
public required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
}
|
|
|
|
public convenience override init() {
|
|
self.init(frame: .zero)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
public override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
|
|
let containerSize = Self.getToggleScaledSize()
|
|
let knobSize = Self.getKnobScaledSize()
|
|
|
|
toggleHeightConstraint?.constant = containerSize.height
|
|
toggleWidthConstraint?.constant = containerSize.width
|
|
|
|
knobHeightConstraint?.constant = knobSize.height
|
|
knobWidthConstraint?.constant = knobSize.width
|
|
|
|
toggleView.layer.cornerRadius = containerSize.height / 2.0
|
|
knobView.layer.cornerRadius = knobSize.height / 2.0
|
|
|
|
resetLabel()
|
|
|
|
changeStateNoAnimation(isOn)
|
|
}
|
|
|
|
public override func setupView() {
|
|
super.setupView()
|
|
|
|
isAccessibilityElement = true
|
|
setAccessibilityHint()
|
|
setAccessibilityLabel()
|
|
accessibilityTraits = .button
|
|
|
|
addSubview(stackView)
|
|
|
|
let containerSize = Self.getToggleScaledSize()
|
|
let knobSize = Self.getKnobScaledSize()
|
|
|
|
toggleHeightConstraint = toggleView.heightAnchor.constraint(equalToConstant: containerSize.height)
|
|
toggleHeightConstraint?.isActive = true
|
|
|
|
toggleWidthConstraint = toggleView.widthAnchor.constraint(equalToConstant: containerSize.width)
|
|
toggleWidthConstraint?.isActive = true
|
|
|
|
toggleView.layer.cornerRadius = containerSize.height / 2.0
|
|
knobView.layer.cornerRadius = knobSize.height / 2.0
|
|
|
|
toggleView.backgroundColor = containerTintColor.off
|
|
|
|
toggleView.addSubview(knobView)
|
|
|
|
knobHeightConstraint = knobView.heightAnchor.constraint(equalToConstant: knobSize.height)
|
|
knobHeightConstraint?.isActive = true
|
|
knobWidthConstraint = knobView.widthAnchor.constraint(equalToConstant: knobSize.width)
|
|
knobWidthConstraint?.isActive = true
|
|
knobView.centerYAnchor.constraint(equalTo: toggleView.centerYAnchor).isActive = true
|
|
knobView.topAnchor.constraint(greaterThanOrEqualTo: toggleView.topAnchor).isActive = true
|
|
toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true
|
|
|
|
//setup stackview
|
|
if showText {
|
|
stackView.addArrangedSubview(label)
|
|
}
|
|
resetLabel()
|
|
stackView.addArrangedSubview(toggleView)
|
|
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
|
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
|
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
|
|
|
constrainKnobOff()
|
|
}
|
|
|
|
func resetLabel() {
|
|
stackView.spacing = showTextSpacing
|
|
if showText {
|
|
if textPosition == .left {
|
|
stackView.insertArrangedSubview(label, at: 0)
|
|
} else {
|
|
stackView.addArrangedSubview(label)
|
|
}
|
|
} else if stackView.subviews.contains(label) {
|
|
label.removeFromSuperview()
|
|
}
|
|
}
|
|
|
|
public override func reset() {
|
|
super.reset()
|
|
|
|
toggleView.backgroundColor = containerTintColor.off
|
|
knobView.backgroundColor = knobTintColor.off
|
|
setAccessibilityLabel()
|
|
isAnimated = true
|
|
onChange = nil
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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()
|
|
}
|
|
|
|
/// This will toggle the state of the Toggle and execute the actionBlock if provided.
|
|
public func toggleAndAction() {
|
|
isOn.toggle()
|
|
onChange?()
|
|
}
|
|
|
|
private func changeStateNoAnimation(_ state: Bool) {
|
|
|
|
// Hold state in case User wanted isAnimated to remain off.
|
|
let isAnimatedState = isAnimated
|
|
|
|
isAnimated = false
|
|
isOn = state
|
|
isAnimated = isAnimatedState
|
|
}
|
|
|
|
override open func accessibilityActivate() -> Bool {
|
|
// Hold state in case User wanted isAnimated to remain off.
|
|
guard isUserInteractionEnabled else { return false }
|
|
let isAnimatedState = isAnimated
|
|
isAnimated = false
|
|
sendActions(for: .touchUpInside)
|
|
isAnimated = isAnimatedState
|
|
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.
|
|
guard let coordinates = touches.first?.location(in: self),
|
|
coordinates.x > -20,
|
|
coordinates.x < bounds.width + 20,
|
|
coordinates.y > -20,
|
|
coordinates.y < bounds.height + 20
|
|
else { return }
|
|
|
|
sendActions(for: .touchUpInside)
|
|
}
|
|
|
|
public func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
|
|
knobReformAnimation()
|
|
sendActions(for: .touchCancel)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Animations
|
|
//--------------------------------------------------
|
|
|
|
public func setToggleAppearanceFromState() {
|
|
|
|
toggleView.backgroundColor = isEnabled ? isOn ? containerTintColor.on : containerTintColor.off : disabledTintColor.container
|
|
knobView.backgroundColor = isEnabled ? isOn ? knobTintColor.on : knobTintColor.off : disabledTintColor.knob
|
|
}
|
|
|
|
public func knobReformAnimation() {
|
|
let knobWidth = Self.getKnobScaledSize().width
|
|
|
|
if isAnimated {
|
|
UIView.animate(withDuration: 0.1, animations: {
|
|
self.knobWidthConstraint?.constant = knobWidth
|
|
self.layoutIfNeeded()
|
|
}, completion: nil)
|
|
|
|
} else {
|
|
knobWidthConstraint?.constant = knobWidth
|
|
layoutIfNeeded()
|
|
}
|
|
}
|
|
|
|
// MARK:- MoleculeViewProtocol
|
|
open override func set(with model: ModelType) {
|
|
self.model = model
|
|
isOn = model.on
|
|
changeStateNoAnimation(isOn)
|
|
isAnimated = true
|
|
isEnabled = !model.disabled
|
|
|
|
guard let font = try? VDSFontStyle.font(for: .body, fontWeight: model.fontWeight, fontSize: model.fontSize) else {
|
|
return
|
|
}
|
|
|
|
label.font = font
|
|
}
|
|
}
|
|
|