switch
This commit is contained in:
parent
14b01d0ae0
commit
af81b62f93
@ -8,13 +8,16 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public class Switch: ViewConstrainingView {
|
public class Switch: ViewConstrainingView, FormValidationProtocol {
|
||||||
|
var aSwitch = UISwitch()
|
||||||
|
var label = Label()
|
||||||
var isRequired: Bool? = false
|
var isRequired: Bool? = false
|
||||||
var state: Bool? = false
|
var state: Bool? = false
|
||||||
var onTintColor: UIColor? = .clear
|
var onTintColor: UIColor? = .clear
|
||||||
var offTintColor: UIColor? = .clear
|
var offTintColor: UIColor? = .clear
|
||||||
var isOnState: Bool? = false
|
var isOnState: Bool? = false
|
||||||
var fieldKey: String? = ""
|
var fieldKey: String? = ""
|
||||||
|
|
||||||
// MARK: - Inits
|
// MARK: - Inits
|
||||||
public init() {
|
public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
@ -33,10 +36,47 @@ public class Switch: ViewConstrainingView {
|
|||||||
super.setAsMolecule()
|
super.setAsMolecule()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func switchChanged(mySwitch: UISwitch) {
|
||||||
|
let value = mySwitch.isOn
|
||||||
|
let str = value ? "Switch is On" : "Switch is Off"
|
||||||
|
print("\(str)")
|
||||||
|
// Do something
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
open override func setupView() {
|
||||||
|
super.setupView()
|
||||||
|
aSwitch.onTintColor = self.onTintColor
|
||||||
|
aSwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)
|
||||||
|
self.addSubview(aSwitch)
|
||||||
|
self.addSubview(label)
|
||||||
|
label.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
aSwitch.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public override func updateView(_ size: CGFloat) {
|
||||||
|
super.updateView(size)
|
||||||
|
self.setupConstraints(forView: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
self.isRequired = json?[KeyRequired] as? Bool
|
self.isRequired = json?[KeyRequired] as? Bool
|
||||||
self.state = json?["state"] as? Bool
|
self.state = json?["state"] as? Bool
|
||||||
|
if let dict = json?.optionalDictionaryForKey("label") {
|
||||||
|
label.text = dict["text"] as? String
|
||||||
|
if let textColor = json?.optionalStringForKey("textColor") {
|
||||||
|
label.textColor = .mfGet(forHex: textColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
||||||
|
FormValidator.setupValidation(molecule: self, delegate: delegateObject.formValidationProtocol)
|
||||||
|
}
|
||||||
|
|
||||||
if let onColorString = json?.optionalStringForKey("onTintColor") {
|
if let onColorString = json?.optionalStringForKey("onTintColor") {
|
||||||
self.onTintColor = .mfGet(forHex: onColorString)
|
self.onTintColor = .mfGet(forHex: onColorString)
|
||||||
}
|
}
|
||||||
@ -44,35 +84,70 @@ public class Switch: ViewConstrainingView {
|
|||||||
self.offTintColor = .mfGet(forHex: offColorString)
|
self.offTintColor = .mfGet(forHex: offColorString)
|
||||||
}
|
}
|
||||||
self.fieldKey = json?[KeyFieldKey] as? String
|
self.fieldKey = json?[KeyFieldKey] as? String
|
||||||
|
aSwitch.onTintColor = self.onTintColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupConstraints(forView view: UIView) {
|
func setupConstraints(forView view: UIView) {
|
||||||
leftPin = view.leftAnchor.constraint(equalTo: leftAnchor)
|
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
|
||||||
topPin = view.topAnchor.constraint(equalTo: topAnchor)
|
label.trailingAnchor.constraint(equalTo: aSwitch.leadingAnchor, constant: 20).isActive = true
|
||||||
rightPin = rightAnchor.constraint(equalTo: view.rightAnchor)
|
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
||||||
bottomPin = bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
label.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
||||||
leftPin?.isActive = true
|
|
||||||
topPin?.isActive = true
|
aSwitch.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: PaddingOne).isActive = true
|
||||||
rightPin?.isActive = true
|
//aSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true
|
||||||
bottomPin?.isActive = true
|
|
||||||
|
|
||||||
|
view.trailingAnchor.constraint(equalTo: aSwitch.trailingAnchor, constant: 0).isActive = true
|
||||||
|
|
||||||
|
|
||||||
|
aSwitch.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
||||||
|
aSwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
||||||
|
// aSwitch.widthAnchor.constraint(equalToConstant: 60.0).isActive = true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - FormValidationProtocol
|
// MARK: - FormValidationProtocol
|
||||||
|
|
||||||
func isValidField() -> Bool {
|
|
||||||
if self.isRequired ?? false {
|
// {
|
||||||
return self.isOnState ?? false
|
// "moleculeName":"switch",
|
||||||
}
|
// "fieldName": "switchOption",
|
||||||
return true
|
// "fieldKey": "switchImei1",
|
||||||
|
// "onTintColor": "#ffff00",
|
||||||
|
// "offTintColor": "#ffffff",
|
||||||
|
// "state": true,
|
||||||
|
// "label": {
|
||||||
|
// "moleculeName": "label",
|
||||||
|
// "text": "This is a switch",
|
||||||
|
// "textColor": "#000000",
|
||||||
|
// "backgroundColor": "#FFFFFF"
|
||||||
|
// },
|
||||||
|
// "required": false,
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
public func isValidField() -> Bool {
|
||||||
|
// if self.isRequired ?? false {
|
||||||
|
// return aSwitch.isOn
|
||||||
|
// }
|
||||||
|
return aSwitch.isOn
|
||||||
}
|
}
|
||||||
|
|
||||||
func formFieldName() -> String {
|
public func formFieldName() -> String? {
|
||||||
return self.fieldKey ?? ""
|
return self.fieldKey ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func formFieldValue() -> Bool {
|
public func formFieldValue() -> Any? {
|
||||||
return self.isOnState ?? false
|
return aSwitch.isOn
|
||||||
|
}
|
||||||
|
|
||||||
|
public override func needsToBeConstrained() -> Bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
public override func moleculeAlignment() -> UIStackView.Alignment {
|
||||||
|
return UIStackView.Alignment.leading
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user