refactored iconOffset
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
e065ead02a
commit
c3aafdea48
@ -29,6 +29,15 @@ open class ButtonIcon: Control {
|
|||||||
case small
|
case small
|
||||||
|
|
||||||
public var defaultValue: Icon.Size { .large }
|
public var defaultValue: Icon.Size { .large }
|
||||||
|
|
||||||
|
public var containerSize: CGFloat {
|
||||||
|
switch self {
|
||||||
|
case .large:
|
||||||
|
return 44.0
|
||||||
|
case .small:
|
||||||
|
return 32.0
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -54,12 +63,11 @@ open class ButtonIcon: Control {
|
|||||||
open var customSize: Int? { didSet { setNeedsUpdate() }}
|
open var customSize: Int? { didSet { setNeedsUpdate() }}
|
||||||
open var floating: Bool = false { didSet { setNeedsUpdate() } }
|
open var floating: Bool = false { didSet { setNeedsUpdate() } }
|
||||||
open var hideBorder: Bool = true { didSet { setNeedsUpdate() } }
|
open var hideBorder: Bool = true { didSet { setNeedsUpdate() } }
|
||||||
open var iconOffset: CGPoint? { didSet { setNeedsUpdate() } }
|
open var iconOffset: CGPoint = .init(x: 0, y: 0) { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Configuration
|
// MARK: - Configuration
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var padding: CGFloat = 10.0
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
@ -85,9 +93,9 @@ open class ButtonIcon: Control {
|
|||||||
addSubview(containerView)
|
addSubview(containerView)
|
||||||
containerView.addSubview(icon)
|
containerView.addSubview(icon)
|
||||||
|
|
||||||
containerView.pinToSuperView(.init(top: padding, left: padding, bottom: padding, right: padding))
|
containerView.pinToSuperView()
|
||||||
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.value.dimensions.width + padding)
|
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.containerSize)
|
||||||
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.value.dimensions.height + padding)
|
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.containerSize)
|
||||||
|
|
||||||
iconCenterXConstraint = icon.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0)
|
iconCenterXConstraint = icon.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0)
|
||||||
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
|
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
|
||||||
@ -103,7 +111,7 @@ open class ButtonIcon: Control {
|
|||||||
size = .large
|
size = .large
|
||||||
floating = false
|
floating = false
|
||||||
hideBorder = true
|
hideBorder = true
|
||||||
iconOffset = nil
|
iconOffset = .init(x: 0, y: 0)
|
||||||
iconName = nil
|
iconName = nil
|
||||||
shouldUpdateView = true
|
shouldUpdateView = true
|
||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
@ -132,28 +140,21 @@ open class ButtonIcon: Control {
|
|||||||
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
|
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
|
||||||
let borderWidth = 2.0
|
let borderWidth = 2.0
|
||||||
let cornerRadius = min(frame.width, frame.height) / 2.0
|
let cornerRadius = min(frame.width, frame.height) / 2.0
|
||||||
|
|
||||||
if let iconOffset {
|
|
||||||
// calculate center point for child view with offset
|
|
||||||
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
|
|
||||||
iconCenterXConstraint?.constant = childCenter.x - containerView.center.x
|
|
||||||
iconCenterYConstraint?.constant = childCenter.y - containerView.center.y
|
|
||||||
} else {
|
|
||||||
iconCenterXConstraint?.constant = 0
|
|
||||||
iconCenterYConstraint?.constant = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// calculate center point for child view with offset
|
||||||
|
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
|
||||||
|
iconCenterXConstraint?.constant = childCenter.x - containerView.center.x
|
||||||
|
iconCenterYConstraint?.constant = childCenter.y - containerView.center.y
|
||||||
|
|
||||||
|
// calculate the icon's container size ensuring the padding
|
||||||
if let customSize {
|
if let customSize {
|
||||||
containerViewWidthConstraint?.constant = CGFloat(customSize) + padding
|
containerViewWidthConstraint?.constant = CGFloat(customSize)
|
||||||
containerViewHeightConstraint?.constant = CGFloat(customSize) + padding
|
containerViewHeightConstraint?.constant = CGFloat(customSize)
|
||||||
} else {
|
} else {
|
||||||
containerViewWidthConstraint?.constant = size.value.dimensions.width + padding
|
containerViewWidthConstraint?.constant = size.containerSize
|
||||||
containerViewHeightConstraint?.constant = size.value.dimensions.height + padding
|
containerViewHeightConstraint?.constant = size.containerSize
|
||||||
}
|
}
|
||||||
|
|
||||||
print("containerViewWidthConstraint :\(containerViewWidthConstraint!.constant)")
|
|
||||||
print("containerViewHeightConstraint :\(containerViewHeightConstraint!.constant)")
|
|
||||||
|
|
||||||
//container
|
//container
|
||||||
backgroundColor = bgColor
|
backgroundColor = bgColor
|
||||||
layer.borderColor = borderColor.cgColor
|
layer.borderColor = borderColor.cgColor
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user