Improved molecular logic of checkbox.

This commit is contained in:
Kevin G Christiano 2019-10-07 16:30:14 -04:00
parent 62527dfd81
commit 7d6289463d
3 changed files with 46 additions and 28 deletions

View File

@ -80,8 +80,10 @@ import MVMCore
/// Width of the check mark.
public var checkWidth: CGFloat = 2 {
didSet {
CATransaction.withDisabledAnimations {
shapeLayer?.lineWidth = checkWidth
if shapeLayer != nil {
CATransaction.withDisabledAnimations {
shapeLayer?.lineWidth = checkWidth
}
}
}
}
@ -89,8 +91,10 @@ import MVMCore
/// Color of the check mark.
public var checkColor: UIColor = .black {
didSet {
CATransaction.withDisabledAnimations {
shapeLayer?.strokeColor = checkColor.cgColor
if shapeLayer != nil {
CATransaction.withDisabledAnimations {
shapeLayer?.strokeColor = checkColor.cgColor
}
}
}
}
@ -109,8 +113,11 @@ import MVMCore
}
}
/// The represented state of the Checkbox.
/// If updateSelectionOnly = true, then this will behave only as a stored property.
/**
The represented state of the Checkbox.
Setting updateSelectionOnly to true bypasses the animation logic inherent with setting this property.
*/
override open var isSelected: Bool {
didSet {
if !updateSelectionOnly {
@ -181,7 +188,7 @@ import MVMCore
layer.borderColor = borderColor.cgColor
}
public func setupView() {
open func setupView() {
isUserInteractionEnabled = true
translatesAutoresizingMaskIntoConstraints = false
@ -225,7 +232,7 @@ import MVMCore
shapeLayer.strokeColor = checkColor.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.path = checkMarkPath()
shapeLayer.lineJoin = .bevel
shapeLayer.lineJoin = .miter
shapeLayer.lineWidth = checkWidth
CATransaction.withDisabledAnimations {
@ -237,7 +244,7 @@ import MVMCore
/// Returns a UIBezierPath detailing the path of a checkmark
func checkMarkPath() -> CGPath {
let sideLength = bounds.size.height
let sideLength = max(bounds.size.height, bounds.size.width)
let startPoint = CGPoint(x: 0.33871 * sideLength, y: 0.53225 * sideLength)
let pivotOffSet = CGPoint(x: 0.46774 * sideLength, y: 0.64516 * sideLength)
let endOffset = CGPoint(x: 0.66935 * sideLength , y: 0.37097 * sideLength)
@ -336,16 +343,27 @@ import MVMCore
open func reset() {
backgroundColor = nil
shapeLayer?.removeAllAnimations()
shapeLayer?.removeFromSuperlayer()
shapeLayer = nil
backgroundColor = nil
borderColor = .black
borderWidth = 1.0
checkColor = .black
checkWidth = 2.0
updateSelectionOnly = true
isSelected = false
updateSelectionOnly = false
}
open func setAsMolecule() {
setupView()
}
public func updateView(_ size: CGFloat) { }
public func updateView(_ size: CGFloat) {
layoutIfNeeded()
}
public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
self.delegateObject = delegateObject

View File

@ -38,6 +38,7 @@
var checkboxTopConstraint: NSLayoutConstraint?
var checkboxBottomConstraint: NSLayoutConstraint?
var checkboxCenterYConstraint: NSLayoutConstraint?
var centerLabelCheckboxConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Life Cycle
@ -62,14 +63,8 @@
checkboxHeightConstraint?.isActive = true
checkbox.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
let generalTop = checkbox.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor)
generalTop.priority = UILayoutPriority(800)
generalTop.isActive = true
let generalBottom = checkbox.bottomAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.bottomAnchor)
generalBottom.priority = UILayoutPriority(800)
generalBottom.isActive = true
checkbox.topAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.topAnchor).isActive = true
layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: checkbox.bottomAnchor).isActive = true
let checboxBottom = checkbox.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
checboxBottom.priority = UILayoutPriority(249)
@ -79,20 +74,17 @@
checkboxBottomConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: checkbox.bottomAnchor)
checkboxTopConstraint = checkbox.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor)
checkboxCenterYConstraint = checkbox.centerYAnchor.constraint(equalTo: centerYAnchor)
centerLabelCheckboxConstraint = label.centerYAnchor.constraint(equalTo: checkbox.centerYAnchor)
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
layoutMarginsGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: PaddingTwo).isActive = true
let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor)
bottomLabelConstraint.priority = UILayoutPriority(500)
layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor)
bottomLabelConstraint.priority = UILayoutPriority(249)
bottomLabelConstraint.isActive = true
// layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
// let labelBottom = label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
// labelBottom.priority = UILayoutPriority(249)
// labelBottom.isActive = true
alignCheckbox(.center)
}
@ -130,19 +122,22 @@
switch position {
case .center:
checkboxCenterYConstraint?.isActive = true
checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = false
checkboxCenterYConstraint?.isActive = true
centerLabelCheckboxConstraint?.isActive = true
case .top:
checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = true
checkboxCenterYConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
case .bottom:
checkboxBottomConstraint?.isActive = true
checkboxTopConstraint?.isActive = false
checkboxCenterYConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
}
}
}
@ -176,6 +171,11 @@ extension CheckboxWithLabelView {
open override func resetConstraints() {
super.resetConstraints()
checkboxCenterYConstraint?.isActive = false
checkboxBottomConstraint?.isActive = false
checkboxTopConstraint?.isActive = false
centerLabelCheckboxConstraint?.isActive = false
}
open override func reset() {

View File

@ -39,10 +39,10 @@
@"textField" : MFTextField.class,
@"digitTextField" : MFDigitTextField.class,
@"checkbox" : Checkbox.class,
@"checkboxWithLabelView" : CheckboxWithLabelView.class,
@"cornerLabels" : CornerLabels.class,
@"progressBar": ProgressBar.class,
@"multiProgressBar": MultiProgress.class,
@"checkboxWithLabelView": CheckboxWithLabelView.class,
@"listItem": MoleculeTableViewCell.class,
@"accordionListItem": AccordionMoleculeTableViewCell.class,
@"switch": MVMCoreUISwitch.class,