Missed a few "fill" types and refactored to use a multiplier

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-07-18 15:34:03 -05:00
parent bf40368dc4
commit b91017068c

View File

@ -185,6 +185,7 @@ open class TileContainerBase<PaddingType: DefaultValuing>: Control where Padding
//-------------------------------------------------- //--------------------------------------------------
internal var widthConstraint: NSLayoutConstraint? internal var widthConstraint: NSLayoutConstraint?
internal var heightConstraint: NSLayoutConstraint? internal var heightConstraint: NSLayoutConstraint?
internal var aspectRatioConstraint: NSLayoutConstraint?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Configuration // MARK: - Configuration
@ -306,6 +307,7 @@ open class TileContainerBase<PaddingType: DefaultValuing>: Control where Padding
contentView.pinToSuperView(.uniform(padding.value)) contentView.pinToSuperView(.uniform(padding.value))
updateContainerView() updateContainerView()
} }
open override var accessibilityElements: [Any]? { open override var accessibilityElements: [Any]? {
@ -380,54 +382,9 @@ open class TileContainerBase<PaddingType: DefaultValuing>: Control where Padding
} }
} }
private func ratioSize(for width: CGFloat) -> CGSize {
var height: CGFloat = width
switch aspectRatio {
case .ratio1x1:
break;
case .ratio3x4:
height = (4 / 3) * width
case .ratio4x3:
height = (3 / 4) * width
case .ratio2x3:
height = (3 / 2) * width
case .ratio3x2:
height = (2 / 3) * width
case .ratio9x16:
height = (16 / 9) * width
case .ratio16x9:
height = (9 / 16) * width
case .ratio1x2:
height = (2 / 1) * width
case .ratio2x1:
height = (1 / 2) * width
default:
break
}
return CGSize(width: width, height: height)
}
private func sizeContainerView(width: CGFloat? = nil, height: CGFloat? = nil) {
if let width, width > 0 {
widthConstraint?.constant = width
widthConstraint?.activate()
}
if let height, height > 0 {
heightConstraint?.constant = height
heightConstraint?.activate()
}
}
private func updateContainerView() { private func updateContainerView() {
applyBackgroundEffects() applyBackgroundEffects()
widthConstraint?.deactivate()
heightConstraint?.deactivate()
if showDropShadow, surface == .light { if showDropShadow, surface == .light {
containerView.addDropShadow(dropShadowConfiguration) containerView.addDropShadow(dropShadowConfiguration)
} else { } else {
@ -437,49 +394,99 @@ open class TileContainerBase<PaddingType: DefaultValuing>: Control where Padding
containerView.dropShadowLayers?.forEach { $0.frame = containerView.bounds } containerView.dropShadowLayers?.forEach { $0.frame = containerView.bounds }
containerView.gradientLayers?.forEach { $0.frame = containerView.bounds } containerView.gradientLayers?.forEach { $0.frame = containerView.bounds }
if width != nil || height != nil { //sizing the container with constraints
var containerViewWidth: CGFloat?
var containerViewHeight: CGFloat?
//run logic to determine which to activate
if let width, aspectRatio == .none && height == nil{
containerViewWidth = width
} else if let height, aspectRatio == .none && width == nil{ //Set local vars
containerViewHeight = height var containerViewWidth: CGFloat? = width
let containerViewHeight: CGFloat? = height
let multiplier = aspectRatio.multiplier
} else if let height, let width { //turn off the constraints
containerViewWidth = width aspectRatioConstraint?.deactivate()
containerViewHeight = height widthConstraint?.deactivate()
heightConstraint?.deactivate()
} else if let width { //-------------------------------------------------------------------------
let size = ratioSize(for: width) //Overriding Nil Width Rules
containerViewWidth = size.width //-------------------------------------------------------------------------
containerViewHeight = size.height //Rule 1:
//In the scenario where we only have a height but the multiplie is nil, we
//want to set the width with the parent's width which will more or less "fill"
//the container horizontally
//- height is set
//- width is not set
//- aspectRatio is not set
if let superviewWidth, superviewWidth > 0,
containerViewHeight != nil,
containerViewWidth == nil,
multiplier == nil {
containerViewWidth = superviewWidth
}
} else if let height { //Rule 2:
let size = ratioSize(for: height) //In the scenario where no width and height is set, want to set the width with the
containerViewWidth = size.width //parent's width which will more or less "fill" the container horizontally
containerViewHeight = size.height //- height is not set
} //- width is not set
sizeContainerView(width: containerViewWidth, height: containerViewHeight) else if let superviewWidth, superviewWidth > 0,
} else { containerViewWidth == nil,
if let parentSize = horizontalPinnedSize() { containerViewHeight == nil {
containerViewWidth = superviewWidth
}
//-------------------------------------------------------------------------
var containerViewWidth: CGFloat?
var containerViewHeight: CGFloat?
let size = ratioSize(for: parentSize.width) //-------------------------------------------------------------------------
if aspectRatio == .none { //Width + AspectRatio Constraint - Will exit out if set
containerViewWidth = size.width //-------------------------------------------------------------------------
} else { if let containerViewWidth,
containerViewWidth = size.width let multiplier,
containerViewHeight = size.height containerViewWidth > 0,
} containerViewHeight == nil {
widthConstraint?.constant = containerViewWidth
widthConstraint?.activate()
aspectRatioConstraint = heightAnchor.constraint(equalTo: widthAnchor, multiplier: multiplier)
aspectRatioConstraint?.activate()
return
}
//-------------------------------------------------------------------------
//Height + AspectRatio Constraint - Will exit out if set
//-------------------------------------------------------------------------
else if let containerViewHeight,
let multiplier,
containerViewHeight > 0,
containerViewWidth == nil {
heightConstraint?.constant = containerViewHeight
heightConstraint?.activate()
aspectRatioConstraint = widthAnchor.constraint(equalTo: heightAnchor, multiplier: multiplier)
aspectRatioConstraint?.activate()
return
}
sizeContainerView(width: containerViewWidth, height: containerViewHeight) //-------------------------------------------------------------------------
} //Width Constraint
//-------------------------------------------------------------------------
if let containerViewWidth,
containerViewWidth > 0 {
widthConstraint?.constant = containerViewWidth
widthConstraint?.activate()
}
//-------------------------------------------------------------------------
//Height Constraint
//-------------------------------------------------------------------------
if let containerViewHeight,
containerViewHeight > 0 {
heightConstraint?.constant = containerViewHeight
heightConstraint?.activate()
} }
} }
/// This is the size of the superview's allowed space for this container first by constrained size which would include padding/inset values an
private var superviewWidth: CGFloat? {
horizontalPinnedWidth() ?? superview?.frame.size.width
}
} }
extension TileContainerBase { extension TileContainerBase {
@ -519,3 +526,30 @@ extension TileContainerBase {
} }
} }
} }
extension TileContainerBase.AspectRatio {
var multiplier: CGFloat? {
switch self {
case .ratio1x1:
return 1
case .ratio3x4:
return 4 / 3
case .ratio4x3:
return 3 / 4
case .ratio2x3:
return 3 / 2
case .ratio3x2:
return 2 / 3
case .ratio9x16:
return 16 / 9
case .ratio16x9:
return 9 / 16
case .ratio1x2:
return 2 / 1
case .ratio2x1:
return 1 / 2
case .none:
return nil
}
}
}