Radio button changes

This commit is contained in:
Pfeil, Scott Robert 2019-10-23 11:32:34 -04:00
parent 7563269680
commit 0b364fa004
4 changed files with 77 additions and 44 deletions

View File

@ -10,7 +10,11 @@ import UIKit
@objcMembers public class RadioButton: Control, FormValidationFormFieldProtocol { @objcMembers public class RadioButton: Control, FormValidationFormFieldProtocol {
var diameter = 30 var diameter: CGFloat = 30 {
didSet {
widthConstraint?.constant = diameter
}
}
var enabledColor = UIColor.black var enabledColor = UIColor.black
var disabledColor = UIColor.mfSilver() var disabledColor = UIColor.mfSilver()
@ -19,6 +23,9 @@ import UIKit
var fieldKey: String? var fieldKey: String?
var formValue: Bool? var formValue: Bool?
var isRequired: Bool = false var isRequired: Bool = false
var widthConstraint: NSLayoutConstraint?
var heightConstraint: NSLayoutConstraint?
lazy var radioGroupName: String? = { lazy var radioGroupName: String? = {
[unowned self] in [unowned self] in
@ -37,12 +44,19 @@ import UIKit
open override func draw(_ rect: CGRect) { open override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return } 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)) let color = isEnabled ? enabledColor.cgColor : disabledColor.cgColor
context.setFillColor(isUserInteractionEnabled ? enabledColor.cgColor : disabledColor.cgColor) layer.cornerRadius = bounds.width * 0.5
context.fillPath() 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() { func tapAction() {
if let radioButtonModel = radioButtonModel { if let radioButtonModel = radioButtonModel {
radioButtonModel.selected(self) radioButtonModel.selected(self)
@ -71,21 +85,24 @@ import UIKit
// MARK: - MVMViewProtocol // MARK: - MVMViewProtocol
open override func setupView() { open override func setupView() {
super.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 clipsToBounds = true
self?.tapAction() 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
} }
} }

View File

@ -314,14 +314,10 @@
[self.molecule updateView:size]; [self.molecule updateView:size];
[MFStyler setMarginsForView:self size:size defaultHorizontal:self.updateViewHorizontalDefaults top:(self.updateViewVerticalDefaults ? self.topMarginPadding : 0) bottom:(self.updateViewVerticalDefaults ? self.bottomMarginPadding : 0)]; [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]; UIEdgeInsets margins = [MVMCoreUIUtility getMarginsForView:self];
if (self.updateViewHorizontalDefaults) { [self setLeftPinConstant:margins.left];
[self setLeftPinConstant:margins.left]; [self setRightPinConstant:margins.right];
[self setRightPinConstant:margins.right]; [self setTopPinConstant:margins.top];
} [self setBottomPinConstant:margins.bottom];
if (self.updateViewVerticalDefaults) {
[self setTopPinConstant:margins.top];
[self setBottomPinConstant:margins.bottom];
}
} }
#pragma mark - MVMCoreUIMoleculeViewProtocol #pragma mark - MVMCoreUIMoleculeViewProtocol

View File

@ -10,20 +10,29 @@ import UIKit
public class Control: UIControl { public class Control: UIControl {
var json: [AnyHashable: Any]? var json: [AnyHashable: Any]?
private var initialSetupPerformed = false
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: .zero) super.init(frame: .zero)
setupView() initialSetup()
} }
init() { init() {
super.init(frame: .zero) super.init(frame: .zero)
setupView() initialSetup()
} }
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {
super.init(coder: coder) 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) { public func updateView(_ size: CGFloat) {
} }
/// Will be called only once.
public func setupView() { public func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false insetsLayoutMarginsFromSafeArea = false

View File

@ -11,26 +11,36 @@ import UIKit
public class View: UIView { public class View: UIView {
var json: [AnyHashable: Any]? var json: [AnyHashable: Any]?
public override init(frame: CGRect) { private var initialSetupPerformed = false
super.init(frame: .zero)
setupView()
}
init() { public override init(frame: CGRect) {
super.init(frame: .zero) super.init(frame: .zero)
setupView() 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 { extension View: MVMCoreViewProtocol {
public func updateView(_ size: CGFloat) { public func updateView(_ size: CGFloat) {
} }
/// Will be called only once.
public func setupView() { public func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false insetsLayoutMarginsFromSafeArea = false