refactored more for changing height/width, offset
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
fe4e9b7a2b
commit
e065ead02a
@ -34,32 +34,32 @@ open class ButtonIcon: Control {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var widthConstraint: NSLayoutConstraint?
|
private var iconCenterXConstraint: NSLayoutConstraint?
|
||||||
private var heightConstraint: NSLayoutConstraint?
|
private var iconCenterYConstraint: NSLayoutConstraint?
|
||||||
|
private var containerViewWidthConstraint: NSLayoutConstraint?
|
||||||
|
private var containerViewHeightConstraint: NSLayoutConstraint?
|
||||||
|
private var containerView = UIView().with {
|
||||||
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
$0.backgroundColor = .clear
|
||||||
|
}
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Properties
|
// MARK: - Public Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open var icon = Icon()
|
open var icon = Icon()
|
||||||
|
|
||||||
open var iconName: Icon.Name? { didSet { setNeedsUpdate() } }
|
|
||||||
|
|
||||||
open var kind: Kind = .ghost { didSet { setNeedsUpdate() } }
|
open var kind: Kind = .ghost { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
open var surfaceType: SurfaceType = .colorFill { didSet { setNeedsUpdate() } }
|
open var surfaceType: SurfaceType = .colorFill { didSet { setNeedsUpdate() } }
|
||||||
|
open var iconName: Icon.Name? { didSet { setNeedsUpdate() } }
|
||||||
open var size: Size = .large { didSet { setNeedsUpdate() } }
|
open var size: Size = .large { 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? { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Configuration
|
// MARK: - Configuration
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
private var padding: CGFloat = 10.0
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
@ -82,7 +82,17 @@ open class ButtonIcon: Control {
|
|||||||
|
|
||||||
open override func setup() {
|
open override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
|
addSubview(containerView)
|
||||||
|
containerView.addSubview(icon)
|
||||||
|
|
||||||
|
containerView.pinToSuperView(.init(top: padding, left: padding, bottom: padding, right: padding))
|
||||||
|
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.value.dimensions.width + padding)
|
||||||
|
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.value.dimensions.height + padding)
|
||||||
|
|
||||||
|
iconCenterXConstraint = icon.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0)
|
||||||
|
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
|
||||||
|
|
||||||
|
NSLayoutConstraint.activate([containerViewWidthConstraint!, containerViewHeightConstraint!, iconCenterXConstraint!, iconCenterYConstraint!])
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
@ -107,10 +117,61 @@ open class ButtonIcon: Control {
|
|||||||
icon.size = size.value
|
icon.size = size.value
|
||||||
icon.surface = surface
|
icon.surface = surface
|
||||||
icon.disabled = disabled
|
icon.disabled = disabled
|
||||||
|
icon.customSize = customSize
|
||||||
} else {
|
} else {
|
||||||
icon.reset()
|
icon.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setNeedsLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
open override func layoutSubviews() {
|
||||||
|
super.layoutSubviews()
|
||||||
|
|
||||||
|
let bgColor = UIColor.red //backgroundColorConfiguration.getColor(self)
|
||||||
|
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
|
||||||
|
let borderWidth = 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
|
||||||
|
}
|
||||||
|
|
||||||
|
if let customSize {
|
||||||
|
containerViewWidthConstraint?.constant = CGFloat(customSize) + padding
|
||||||
|
containerViewHeightConstraint?.constant = CGFloat(customSize) + padding
|
||||||
|
} else {
|
||||||
|
containerViewWidthConstraint?.constant = size.value.dimensions.width + padding
|
||||||
|
containerViewHeightConstraint?.constant = size.value.dimensions.height + padding
|
||||||
|
}
|
||||||
|
|
||||||
|
print("containerViewWidthConstraint :\(containerViewWidthConstraint!.constant)")
|
||||||
|
print("containerViewHeightConstraint :\(containerViewHeightConstraint!.constant)")
|
||||||
|
|
||||||
|
//container
|
||||||
|
backgroundColor = bgColor
|
||||||
|
layer.borderColor = borderColor.cgColor
|
||||||
|
layer.cornerRadius = cornerRadius
|
||||||
|
layer.borderWidth = borderWidth
|
||||||
|
|
||||||
|
//icon
|
||||||
|
icon.layer.borderColor = UIColor.purple.cgColor
|
||||||
|
icon.layer.borderWidth = 2
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: AppleGuidlinesTouchable
|
||||||
|
extension ButtonIcon: AppleGuidlinesTouchable {
|
||||||
|
|
||||||
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
||||||
|
Self.acceptablyOutsideBounds(point: point, bounds: bounds)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user