Merge branch 'feature/checkbox_update' into 'develop'
Few more updates to Checkbox. See merge request BPHV_MIPS/mvm_core_ui!147
This commit is contained in:
commit
b0b0121d49
@ -8,22 +8,6 @@
|
|||||||
|
|
||||||
import MVMCore
|
import MVMCore
|
||||||
|
|
||||||
/*
|
|
||||||
!!! -- DO NOT REMOVE -- !!!
|
|
||||||
(Unless Design changes the appearance of the checkmark).
|
|
||||||
|
|
||||||
// Offsets based on the 124x124 example checkmark
|
|
||||||
let startXOffset: Float = 42.0 / 124.0 ~~ 0.33871
|
|
||||||
let startYOffset: Float = 66.0 / 124.0 ~~ 0.53225
|
|
||||||
let pivotXOffset: Float = 58.0 / 124.0 ~~ 0.46774
|
|
||||||
let pivotYOffset: Float = 80.0 / 124.0 ~~ 0.64516
|
|
||||||
let endXOffset: Float = 83.0 / 124.0 ~~ 0.66935
|
|
||||||
let endYOffset: Float = 46.0 / 124.0 ~~ 0.37097
|
|
||||||
let pivotPercentage: Float = 0.34
|
|
||||||
let endPercentage = 1.0 - pivotPercentage
|
|
||||||
let animationInterval: Float = 0.01
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
This class expects its height and width to be equal.
|
This class expects its height and width to be equal.
|
||||||
*/
|
*/
|
||||||
@ -49,7 +33,7 @@ import MVMCore
|
|||||||
public var updateSelectionOnly: Bool = false
|
public var updateSelectionOnly: Bool = false
|
||||||
|
|
||||||
/// The color of the background when checked.
|
/// The color of the background when checked.
|
||||||
public var checkedBackgroundColor: UIColor = .white {
|
public var checkedBackgroundColor: UIColor = .clear {
|
||||||
didSet {
|
didSet {
|
||||||
if isSelected {
|
if isSelected {
|
||||||
backgroundColor = checkedBackgroundColor
|
backgroundColor = checkedBackgroundColor
|
||||||
@ -58,7 +42,7 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The color of the background when unChecked.
|
/// The color of the background when unChecked.
|
||||||
public var unCheckedBackgroundColor: UIColor = .white {
|
public var unCheckedBackgroundColor: UIColor = .clear {
|
||||||
didSet {
|
didSet {
|
||||||
if !isSelected {
|
if !isSelected {
|
||||||
backgroundColor = unCheckedBackgroundColor
|
backgroundColor = unCheckedBackgroundColor
|
||||||
@ -91,11 +75,7 @@ import MVMCore
|
|||||||
/// Color of the check mark.
|
/// Color of the check mark.
|
||||||
public var checkColor: UIColor = .black {
|
public var checkColor: UIColor = .black {
|
||||||
didSet {
|
didSet {
|
||||||
if let shapeLayer = shapeLayer {
|
setShapeLayerStrokeColor(checkColor)
|
||||||
CATransaction.withDisabledAnimations {
|
|
||||||
shapeLayer.strokeColor = checkColor.cgColor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,6 +115,21 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Constraints
|
||||||
|
//--------------------------------------------------
|
||||||
|
|
||||||
|
private var heightConstraint: NSLayoutConstraint?
|
||||||
|
private var widthConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
|
/// Updates the height and width anchors of the Checkbox with the assigned value.
|
||||||
|
public var heigthWidthConstant: CGFloat = Checkbox.defaultHeightWidth {
|
||||||
|
didSet {
|
||||||
|
heightConstraint?.constant = heigthWidthConstant
|
||||||
|
widthConstraint?.constant = heigthWidthConstant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -145,7 +140,6 @@ import MVMCore
|
|||||||
accessibilityTraits = .button
|
accessibilityTraits = .button
|
||||||
accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "checkbox_action_hint")
|
accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "checkbox_action_hint")
|
||||||
updateAccessibilityLabel()
|
updateAccessibilityLabel()
|
||||||
|
|
||||||
setupView()
|
setupView()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +176,7 @@ import MVMCore
|
|||||||
override open func layoutSubviews() {
|
override open func layoutSubviews() {
|
||||||
super.layoutSubviews()
|
super.layoutSubviews()
|
||||||
|
|
||||||
drawCheck()
|
drawShapeLayer()
|
||||||
layer.cornerRadius = isRound ? cornerRadiusValue : 0
|
layer.cornerRadius = isRound ? cornerRadiusValue : 0
|
||||||
layer.borderWidth = borderWidth
|
layer.borderWidth = borderWidth
|
||||||
layer.borderColor = borderColor.cgColor
|
layer.borderColor = borderColor.cgColor
|
||||||
@ -190,9 +184,15 @@ import MVMCore
|
|||||||
|
|
||||||
open func setupView() {
|
open func setupView() {
|
||||||
|
|
||||||
|
guard constraints.isEmpty else { return }
|
||||||
|
|
||||||
isUserInteractionEnabled = true
|
isUserInteractionEnabled = true
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
translatesAutoresizingMaskIntoConstraints = false
|
||||||
backgroundColor = .white
|
backgroundColor = .clear
|
||||||
|
|
||||||
|
widthConstraint = widthAnchor.constraint(equalToConstant: Checkbox.defaultHeightWidth)
|
||||||
|
heightConstraint = heightAnchor.constraint(equalToConstant: Checkbox.defaultHeightWidth)
|
||||||
|
heightWidthIsActive(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -220,8 +220,8 @@ import MVMCore
|
|||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
/// Creates the check mark used for the checkbox.
|
/// Creates the check mark layer.
|
||||||
private func drawCheck() {
|
private func drawShapeLayer() {
|
||||||
|
|
||||||
if shapeLayer == nil {
|
if shapeLayer == nil {
|
||||||
|
|
||||||
@ -241,13 +241,18 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a UIBezierPath detailing the path of a checkmark
|
/// - returns: The CGPath of a UIBezierPath detailing the path of a checkmark
|
||||||
func checkMarkPath() -> CGPath {
|
func checkMarkPath() -> CGPath {
|
||||||
|
|
||||||
let sideLength = max(bounds.size.height, bounds.size.width)
|
let length = max(bounds.size.height, bounds.size.width)
|
||||||
let startPoint = CGPoint(x: 0.33871 * sideLength, y: 0.53225 * sideLength)
|
let xInsetLeft = length * 0.25
|
||||||
let pivotOffSet = CGPoint(x: 0.46774 * sideLength, y: 0.64516 * sideLength)
|
let yInsetTop = length * 0.3
|
||||||
let endOffset = CGPoint(x: 0.66935 * sideLength , y: 0.37097 * sideLength)
|
let innerWidth = length - (xInsetLeft + length * 0.25) // + Right X Inset
|
||||||
|
let innerHeight = length - (yInsetTop + length * 0.35) // + Bottom Y Inset
|
||||||
|
|
||||||
|
let startPoint = CGPoint(x: xInsetLeft, y: yInsetTop + (innerHeight / 2))
|
||||||
|
let pivotOffSet = CGPoint(x: xInsetLeft + (innerWidth * 0.33), y: yInsetTop + innerHeight)
|
||||||
|
let endOffset = CGPoint(x: xInsetLeft + innerWidth, y: yInsetTop)
|
||||||
|
|
||||||
let bezierPath = UIBezierPath()
|
let bezierPath = UIBezierPath()
|
||||||
bezierPath.move(to: startPoint)
|
bezierPath.move(to: startPoint)
|
||||||
@ -267,14 +272,14 @@ import MVMCore
|
|||||||
self.updateSelectionOnly = true
|
self.updateSelectionOnly = true
|
||||||
self.isSelected = selected
|
self.isSelected = selected
|
||||||
self.updateSelectionOnly = false
|
self.updateSelectionOnly = false
|
||||||
self.drawCheck()
|
self.drawShapeLayer()
|
||||||
self.shapeLayer?.removeAllAnimations()
|
self.shapeLayer?.removeAllAnimations()
|
||||||
self.updateCheckboxUI(isSelected: selected, isAnimated: animated)
|
self.updateCheckboxUI(isSelected: selected, isAnimated: animated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// updates the visuals of the check mark and background.
|
/// updates the visuals of the check mark and background.
|
||||||
/// - parameter isSelection: the check state of the checkbox.
|
/// - parameter isSelected: the check state of the checkbox.
|
||||||
/// - parameter isAnimated: determines of the changes should animate or immediately refelect.
|
/// - parameter isAnimated: determines of the changes should animate or immediately refelect.
|
||||||
public func updateCheckboxUI(isSelected: Bool, isAnimated: Bool) {
|
public func updateCheckboxUI(isSelected: Bool, isAnimated: Bool) {
|
||||||
|
|
||||||
@ -296,7 +301,7 @@ import MVMCore
|
|||||||
self.shapeLayer?.strokeEnd = isSelected ? 1 : 0
|
self.shapeLayer?.strokeEnd = isSelected ? 1 : 0
|
||||||
}
|
}
|
||||||
|
|
||||||
self.backgroundColor = isSelected ? self.checkedBackgroundColor : self.unCheckedBackgroundColor
|
backgroundColor = isSelected ? checkedBackgroundColor : unCheckedBackgroundColor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,6 +313,38 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isEnabled(_ enabled: Bool) {
|
||||||
|
|
||||||
|
isUserInteractionEnabled = enabled
|
||||||
|
|
||||||
|
if enabled {
|
||||||
|
layer.borderColor = borderColor.cgColor
|
||||||
|
backgroundColor = isSelected ? checkedBackgroundColor : unCheckedBackgroundColor
|
||||||
|
alpha = 1.0
|
||||||
|
setShapeLayerStrokeColor(checkColor)
|
||||||
|
} else {
|
||||||
|
layer.borderColor = UIColor.mfSilver().cgColor
|
||||||
|
backgroundColor = .clear
|
||||||
|
alpha = DisableOppacity
|
||||||
|
setShapeLayerStrokeColor(UIColor.mfSilver())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setShapeLayerStrokeColor(_ color: UIColor) {
|
||||||
|
|
||||||
|
if let shapeLayer = shapeLayer {
|
||||||
|
CATransaction.withDisabledAnimations {
|
||||||
|
shapeLayer.strokeColor = color.cgColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func heightWidthIsActive(_ isActive: Bool) {
|
||||||
|
|
||||||
|
heightConstraint?.isActive = isActive
|
||||||
|
widthConstraint?.isActive = isActive
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - UITouch
|
// MARK: - UITouch
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -343,10 +380,11 @@ import MVMCore
|
|||||||
|
|
||||||
open func reset() {
|
open func reset() {
|
||||||
|
|
||||||
|
isEnabled(true)
|
||||||
shapeLayer?.removeAllAnimations()
|
shapeLayer?.removeAllAnimations()
|
||||||
shapeLayer?.removeFromSuperlayer()
|
shapeLayer?.removeFromSuperlayer()
|
||||||
shapeLayer = nil
|
shapeLayer = nil
|
||||||
backgroundColor = nil
|
backgroundColor = .clear
|
||||||
borderColor = .black
|
borderColor = .black
|
||||||
borderWidth = 1.0
|
borderWidth = 1.0
|
||||||
checkColor = .black
|
checkColor = .black
|
||||||
@ -413,6 +451,10 @@ import MVMCore
|
|||||||
self.isRound = isRound
|
self.isRound = isRound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let enabled = dictionary["isEnabled"] as? Bool {
|
||||||
|
isEnabled(enabled)
|
||||||
|
}
|
||||||
|
|
||||||
if let actionMap = dictionary.optionalDictionaryForKey("actionMap") {
|
if let actionMap = dictionary.optionalDictionaryForKey("actionMap") {
|
||||||
actionBlock = { MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) }
|
actionBlock = { MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) }
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user