updated shapelayer

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-02 17:09:38 -05:00
parent d8a55d87bc
commit 7256f0971c

View File

@ -100,16 +100,16 @@ import Combine
private var mainStackView: UIStackView = { private var mainStackView: UIStackView = {
let stackView = UIStackView() let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.alignment = .top
stackView.axis = .vertical stackView.axis = .vertical
stackView.distribution = .fill
return stackView return stackView
}() }()
private var checkBoxStackView: UIStackView = { private var checkboxStackView: UIStackView = {
let stackView = UIStackView() let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.alignment = .top
stackView.axis = .horizontal stackView.axis = .horizontal
stackView.distribution = .fillProportionally
return stackView return stackView
}() }()
@ -117,8 +117,6 @@ import Combine
let stackView = UIStackView() let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical stackView.axis = .vertical
stackView.distribution = .fillProportionally
stackView.spacing = 5
return stackView return stackView
}() }()
@ -242,13 +240,18 @@ import Combine
public override func setupView() { public override func setupView() {
super.setupView() super.setupView()
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(VDSCheckbox.toggleAndAction)))
isAccessibilityElement = true isAccessibilityElement = true
accessibilityTraits = .button accessibilityTraits = .button
addSubview(mainStackView) addSubview(mainStackView)
mainStackView.addArrangedSubview(checkBoxStackView) mainStackView.addArrangedSubview(checkboxStackView)
checkBoxStackView.addArrangedSubview(checkboxView) mainStackView.addArrangedSubview(errorLabel)
checkboxStackView.addArrangedSubview(checkboxView)
checkboxStackView.addArrangedSubview(checkboxLabelStackView)
checkboxLabelStackView.addArrangedSubview(primaryLabel)
checkboxLabelStackView.addArrangedSubview(secondaryLabel)
checkboxHeightConstraint = checkboxView.heightAnchor.constraint(equalToConstant: checkboxSize.height) checkboxHeightConstraint = checkboxView.heightAnchor.constraint(equalToConstant: checkboxSize.height)
checkboxHeightConstraint?.isActive = true checkboxHeightConstraint?.isActive = true
@ -272,10 +275,10 @@ import Combine
let background = getCheckboxBackgroundColor(for: viewModel.disabled, surface: viewModel.surface) let background = getCheckboxBackgroundColor(for: viewModel.disabled, surface: viewModel.surface)
let border = getCheckboxBorderColor(for: viewModel.disabled, surface: viewModel.surface) let border = getCheckboxBorderColor(for: viewModel.disabled, surface: viewModel.surface)
let checkColor = getCheckboxCheckColor(for: viewModel.disabled, surface: viewModel.surface) let checkColor = getCheckboxCheckColor(for: viewModel.disabled, surface: viewModel.surface)
let backgroundColor = viewModel.on ? background.on : background.off
checkboxView.backgroundColor = viewModel.on ? background.on : background.off checkboxView.backgroundColor = backgroundColor
checkboxView.borderColor = viewModel.on ? border.on : border.off checkboxView.borderColor = viewModel.on ? border.on : border.off
checkboxView.checkColor = checkColor checkboxView.checkColor = viewModel.on ? checkColor : backgroundColor
checkboxView.isSelected = viewModel.on checkboxView.isSelected = viewModel.on
} }
@ -284,41 +287,39 @@ import Combine
//deal with labels //deal with labels
if model.shouldShowLabels { if model.shouldShowLabels {
//add the stackview to hold the 2 labels //add the stackview to hold the 2 labels
if checkBoxStackView.subviews.contains(checkboxLabelStackView) == false {
checkBoxStackView.addArrangedSubview(checkboxLabelStackView)
}
//top label //top label
if let labelModel = viewModel.labelModel { if let labelModel = viewModel.labelModel {
primaryLabel.set(with: labelModel) primaryLabel.set(with: labelModel)
if checkboxLabelStackView.subviews.contains(primaryLabel) == false { primaryLabel.isHidden = false
checkboxLabelStackView.insertArrangedSubview(primaryLabel, at: 0)
}
} else { } else {
primaryLabel.removeFromSuperview() primaryLabel.isHidden = true
} }
//bottom label //bottom label
if let childModel = viewModel.childModel { if let childModel = viewModel.childModel {
secondaryLabel.set(with: childModel) secondaryLabel.set(with: childModel)
if checkboxLabelStackView.subviews.contains(secondaryLabel) == false { secondaryLabel.isHidden = false
checkboxLabelStackView.addArrangedSubview(secondaryLabel)
}
} else { } else {
secondaryLabel.removeFromSuperview() secondaryLabel.isHidden = true
} }
checkboxStackView.spacing = 12
checkboxLabelStackView.spacing = 4
checkboxLabelStackView.isHidden = false
} else { } else {
checkboxLabelStackView.removeFromSuperview() checkboxStackView.spacing = 0
checkboxLabelStackView.spacing = 0
checkboxLabelStackView.isHidden = true
} }
//either add/remove the error from the main stack //either add/remove the error from the main stack
if let errorModel = model.errorModel, model.shouldShowError { if let errorModel = model.errorModel, model.shouldShowError {
errorLabel.set(with: errorModel) errorLabel.set(with: errorModel)
if mainStackView.subviews.contains(errorLabel) == false { mainStackView.spacing = 8
mainStackView.spacing = 12 errorLabel.isHidden = false
mainStackView.addArrangedSubview(errorLabel)
}
} else { } else {
mainStackView.spacing = 0 mainStackView.spacing = 0
errorLabel.removeFromSuperview() errorLabel.isHidden = true
} }
} }
@ -360,8 +361,8 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UIResponder // MARK: - UIResponder
//-------------------------------------------------- //--------------------------------------------------
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .touchUpInside) sendActions(for: .touchUpInside)
} }
@ -389,7 +390,13 @@ import Combine
public var checkColor: UIColor = .white public var checkColor: UIColor = .white
public var isSelected: Bool = false { public var isSelected: Bool = false {
didSet { didSet {
drawShapeLayer() if oldValue != isSelected {
if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) {
shapeLayer.removeFromSuperlayer()
self.shapeLayer = nil
}
drawShapeLayer()
}
} }
} }
@ -402,7 +409,7 @@ import Combine
layer.cornerRadius = 2.0 layer.cornerRadius = 2.0
shapeLayer?.strokeColor = checkColor.cgColor shapeLayer?.strokeColor = checkColor.cgColor
guard let path = try? checkMarkPath() else { return } guard let path = try? checkMarkPath(), shapeLayer == nil else { return }
if shapeLayer == nil { if shapeLayer == nil {
let shapeLayer = CAShapeLayer() let shapeLayer = CAShapeLayer()