From 0b364fa004d0e885dd1c81dc3ba602e999cfcfb0 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 23 Oct 2019 11:32:34 -0400 Subject: [PATCH] Radio button changes --- MVMCoreUI/Atoms/Views/RadioButton.swift | 55 +++++++++++++------- MVMCoreUI/Atoms/Views/ViewConstrainingView.m | 12 ++--- MVMCoreUI/BaseClasses/Control.swift | 16 ++++-- MVMCoreUI/BaseClasses/View.swift | 38 +++++++++----- 4 files changed, 77 insertions(+), 44 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/RadioButton.swift b/MVMCoreUI/Atoms/Views/RadioButton.swift index 5b0d0c00..6347a862 100644 --- a/MVMCoreUI/Atoms/Views/RadioButton.swift +++ b/MVMCoreUI/Atoms/Views/RadioButton.swift @@ -10,7 +10,11 @@ import UIKit @objcMembers public class RadioButton: Control, FormValidationFormFieldProtocol { - var diameter = 30 + var diameter: CGFloat = 30 { + didSet { + widthConstraint?.constant = diameter + } + } var enabledColor = UIColor.black var disabledColor = UIColor.mfSilver() @@ -19,6 +23,9 @@ import UIKit var fieldKey: String? var formValue: Bool? var isRequired: Bool = false + + var widthConstraint: NSLayoutConstraint? + var heightConstraint: NSLayoutConstraint? lazy var radioGroupName: String? = { [unowned self] in @@ -37,12 +44,19 @@ import UIKit open override func draw(_ rect: CGRect) { guard let context = UIGraphicsGetCurrentContext() else { return } - context.addEllipse(in: CGRect(x: bounds.width*0.2, y: bounds.height*0.2, width: bounds.width*0.6, height: bounds.height*0.6)) - context.setFillColor(isUserInteractionEnabled ? enabledColor.cgColor : disabledColor.cgColor) - context.fillPath() + let color = isEnabled ? enabledColor.cgColor : disabledColor.cgColor + layer.cornerRadius = bounds.width * 0.5 + layer.borderColor = color + layer.borderWidth = bounds.width * 0.0333 + if isSelected { + // Space around inner circle is 1/5 the size + context.addEllipse(in: CGRect(x: bounds.width*0.2, y: bounds.height*0.2, width: bounds.width*0.6, height: bounds.height*0.6)) + context.setFillColor(color) + context.fillPath() + } } - // MARK: - Inits + /// The action performed when tapped. func tapAction() { if let radioButtonModel = radioButtonModel { radioButtonModel.selected(self) @@ -71,21 +85,24 @@ import UIKit // MARK: - MVMViewProtocol open override func setupView() { super.setupView() - guard subviews.count == 0 else { - return - } - - /*translatesAutoresizingMaskIntoConstraints = false - addSubview(radioButton) - - isAccessibilityElement = true - accessibilityTraits = .none - accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "radio_action_hint") - NSLayoutConstraint.constraintPinSubview(toSuperview: radioButton) - radioButton.performActionForCheck = { [weak self] in - self?.tapAction() - }*/ + clipsToBounds = true + widthConstraint = widthAnchor.constraint(equalToConstant: 30) + widthConstraint?.isActive = true + heightConstraint = heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1) + heightConstraint?.isActive = true + + addTarget(self, action: #selector(tapAction), for: .touchUpInside) + isAccessibilityElement = true + accessibilityTraits = .button + accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "radio_action_hint") + } +} + +// MARK: - MVMCoreUIViewConstrainingProtocol +extension RadioButton: MVMCoreUIViewConstrainingProtocol { + public func needsToBeConstrained() -> Bool { + return true } } diff --git a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m index 66a3a83d..c6b1023e 100644 --- a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m +++ b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m @@ -314,14 +314,10 @@ [self.molecule updateView:size]; [MFStyler setMarginsForView:self size:size defaultHorizontal:self.updateViewHorizontalDefaults top:(self.updateViewVerticalDefaults ? self.topMarginPadding : 0) bottom:(self.updateViewVerticalDefaults ? self.bottomMarginPadding : 0)]; UIEdgeInsets margins = [MVMCoreUIUtility getMarginsForView:self]; - if (self.updateViewHorizontalDefaults) { - [self setLeftPinConstant:margins.left]; - [self setRightPinConstant:margins.right]; - } - if (self.updateViewVerticalDefaults) { - [self setTopPinConstant:margins.top]; - [self setBottomPinConstant:margins.bottom]; - } + [self setLeftPinConstant:margins.left]; + [self setRightPinConstant:margins.right]; + [self setTopPinConstant:margins.top]; + [self setBottomPinConstant:margins.bottom]; } #pragma mark - MVMCoreUIMoleculeViewProtocol diff --git a/MVMCoreUI/BaseClasses/Control.swift b/MVMCoreUI/BaseClasses/Control.swift index bad1a6c8..fdf8204f 100644 --- a/MVMCoreUI/BaseClasses/Control.swift +++ b/MVMCoreUI/BaseClasses/Control.swift @@ -10,20 +10,29 @@ import UIKit public class Control: UIControl { var json: [AnyHashable: Any]? + + private var initialSetupPerformed = false public override init(frame: CGRect) { super.init(frame: .zero) - setupView() + initialSetup() } init() { super.init(frame: .zero) - setupView() + initialSetup() } public required init?(coder: NSCoder) { super.init(coder: coder) - setupView() + initialSetup() + } + + public func initialSetup() { + if !initialSetupPerformed { + initialSetupPerformed = true + setupView() + } } } @@ -31,6 +40,7 @@ extension Control: MVMCoreViewProtocol { public func updateView(_ size: CGFloat) { } + /// Will be called only once. public func setupView() { translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false diff --git a/MVMCoreUI/BaseClasses/View.swift b/MVMCoreUI/BaseClasses/View.swift index 25b750b0..735243df 100644 --- a/MVMCoreUI/BaseClasses/View.swift +++ b/MVMCoreUI/BaseClasses/View.swift @@ -11,26 +11,36 @@ import UIKit public class View: UIView { var json: [AnyHashable: Any]? - public override init(frame: CGRect) { - super.init(frame: .zero) - setupView() - } + private var initialSetupPerformed = false - init() { - super.init(frame: .zero) - setupView() + public override init(frame: CGRect) { + super.init(frame: .zero) + initialSetup() + } + + init() { + super.init(frame: .zero) + initialSetup() + } + + public required init?(coder: NSCoder) { + super.init(coder: coder) + initialSetup() + } + + public func initialSetup() { + if !initialSetupPerformed { + initialSetupPerformed = true + setupView() + } + } } - - public required init?(coder: NSCoder) { - super.init(coder: coder) - setupView() - } -} extension View: MVMCoreViewProtocol { public func updateView(_ size: CGFloat) { } - + + /// Will be called only once. public func setupView() { translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false