This commit is contained in:
Kevin G Christiano 2019-12-10 10:34:08 -05:00
parent dd47707064
commit 83370956c1

View File

@ -9,8 +9,6 @@
import MVMCore
import UIKit
public typealias ValueChangeBlock = () -> ()
/**
A custom implementation of Apple's UISwitch.
@ -27,11 +25,11 @@ public typealias ValueChangeBlock = () -> ()
public var disabledTintColor: (track: UIColor?, thumb: UIColor?)? = (track: .mfSilver(), thumb: .white)
private var valueShouldChange = false
private var canChangeValue = false
// private var canChangeValue = false
private var shouldTouchToSwitch = false
private var valueChangedBlock: ValueChangeBlock?
private var actionBlock: ValueChangeBlock?
private var valueChangedBlock: ActionBlock?
private var actionBlock: ActionBlock?
// Sizes are from InVision design specs.
static let trackSize = CGSize(width: 46, height: 24)
@ -90,13 +88,13 @@ public typealias ValueChangeBlock = () -> ()
self.init(frame: .zero)
}
public convenience init(isOn: Bool, changeBlock: ValueChangeBlock?) {
public convenience init(isOn: Bool, changeBlock: ActionBlock?) {
self.init(frame: .zero)
self.isOn = isOn
valueChangedBlock = changeBlock
}
public convenience init(changeBlock: ValueChangeBlock?) {
public convenience init(changeBlock: ActionBlock?) {
self.init(frame: .zero)
valueChangedBlock = changeBlock
}
@ -116,19 +114,19 @@ public typealias ValueChangeBlock = () -> ()
heightConstraint?.constant = Switch.getTrackHeight()
widthConstraint?.constant = Switch.getTrackWidth()
layer.cornerRadius = Switch.getTrackHeight() / 2.0
thumbView.layer.cornerRadius = Switch.getThumbHeight() / 2.0
thumbHeightConstraint?.constant = Switch.getThumbHeight()
thumbWidthConstraint?.constant = Switch.getThumbWidth()
layer.cornerRadius = Switch.getTrackHeight() / 2.0
thumbView.layer.cornerRadius = Switch.getThumbHeight() / 2.0
}
public override func setupView() {
super.setupView()
guard !subviews.isEmpty else { return }
guard subviews.isEmpty else { return }
canChangeValue = true
// canChangeValue = true
shouldTouchToSwitch = true
valueShouldChange = true
@ -263,11 +261,6 @@ public typealias ValueChangeBlock = () -> ()
valueShouldChange = true
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .touchUpInside)
}
public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
if shouldTouchToSwitch {
@ -304,14 +297,13 @@ public typealias ValueChangeBlock = () -> ()
if shouldTouchToSwitch {
thumbReformAnimation(true)
canChangeValue = true
}
sendActions(for: .touchCancel)
}
//--------------------------------------------------
// MARK: - Animation
// MARK: - Animations
//--------------------------------------------------
public func thumbEnlargeAnimation() {
@ -443,10 +435,10 @@ extension Switch {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
self.delegateObject = delegateObject
guard let dictionary = json else { return }
FormValidator.setupValidation(molecule: self, delegate: delegateObject?.formValidationProtocol)
guard let dictionary = json else { return }
if let color = dictionary["onTintColor"] as? String {
trackTintColor?.on = UIColor.mfGet(forHex: color)
}