udpated swatch

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-09-09 12:52:34 -05:00
parent 2a53bae78c
commit a07fc10a7e
2 changed files with 44 additions and 9 deletions

View File

@ -32,12 +32,13 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
}
}()
public var fillView: UIView = {
return UIView().with {
public var fillView: UIImageView = {
return UIImageView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.contentMode = .scaleAspectFit
}
}()
public var onChange: Blocks.ActionBlock?
//can't bind to @Proxy
@ -168,7 +169,7 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
private var selectorBorderWidth: CGFloat = 1.0
public let swatchSize = CGSize(width: 48, height: 48)
public let fillSize = CGSize(width: 36, height: 36)
public let disabledAlpha = 0.5
private var radioSwatchBackgroundColorConfiguration: BinaryDisabledSurfaceColorConfiguration<ModelType> = {
return BinaryDisabledSurfaceColorConfiguration<ModelType>().with {
$0.forFalse.enabled.lightColor = VDSFormControlsColor.backgroundOnlight
@ -210,6 +211,7 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
/// Manages the appearance of the radioSwatch.
private var shapeLayer: CAShapeLayer?
private var gradientLayer: CAGradientLayer?
open func getSelectorSize() -> CGSize {
return swatchSize
@ -226,12 +228,35 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
selectorView.layer.borderWidth = viewModel.selected ? selectorBorderWidth : 0
selectorView.layer.masksToBounds = true
var fillColor: UIColor = viewModel.primaryColor ?? .white
fillView.backgroundColor = fillColor
gradientLayer?.removeFromSuperlayer()
gradientLayer = nil
var fillColorBackground: UIColor = .clear
if let fillImage = viewModel.fillImage {
fillView.image = viewModel.disabled ? fillImage.image(alpha: disabledAlpha) : fillImage
} else {
fillView.image = nil
if let primary = viewModel.primaryColor, let secondary = viewModel.secondaryColor {
let firstColor = viewModel.disabled ? primary.withAlphaComponent(disabledAlpha) : primary
let secondColor = viewModel.disabled ? secondary.withAlphaComponent(disabledAlpha) : secondary
let gradient = CAGradientLayer()
gradientLayer = gradient
gradient.frame = fillView.bounds
gradient.colors = [secondColor.cgColor, secondColor.cgColor, firstColor.cgColor, firstColor.cgColor]
gradient.locations = [NSNumber(value: 0.0), NSNumber(value: 0.5), NSNumber(value: 0.5), NSNumber(value: 1.0)]
gradient.transform = CATransform3DMakeRotation(135.0 / 180.0 * .pi, 0.0, 0.0, 1.0)
fillView.layer.addSublayer(gradient)
} else {
fillColorBackground = viewModel.primaryColor ?? .white
}
}
fillView.backgroundColor = viewModel.disabled ? fillColorBackground.withAlphaComponent(disabledAlpha) : fillColorBackground
fillView.layer.borderColor = fillBorderColor.cgColor
fillView.layer.cornerRadius = fillView.bounds.width * 0.5
fillView.layer.borderWidth = selectorBorderWidth
fillView.layer.masksToBounds = true
setNeedsDisplay()
}
@ -270,3 +295,13 @@ open class RadioSwatchBase<ModelType: RadioSwatchModel>: Control<ModelType>, Cha
}
}
}
extension UIImage {
func image(alpha: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: .zero, blendMode: .normal, alpha: alpha)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}

View File

@ -9,7 +9,7 @@ import Foundation
import UIKit
public protocol RadioSwatchModel: Modelable, FormFieldable, DataTrackable, Accessable, Selectable, BinaryColorable, Equatable {
var fillImage: String? { get set }
var fillImage: UIImage? { get set }
var primaryColor: UIColor? { get set }
var secondaryColor: UIColor? { get set }
var text: String { get set }
@ -41,7 +41,7 @@ public struct DefaultRadioSwatchModel: RadioSwatchModel {
public var selected: Bool = false
public var fillImage: String?
public var fillImage: UIImage?
public var primaryColor: UIColor?
public var secondaryColor: UIColor?
public var text: String = ""