SegmentedReliefButton: actually show background states

This commit is contained in:
James Magahern
2020-08-14 18:42:40 -07:00
parent fa4ba64660
commit f2190abb6e
3 changed files with 78 additions and 13 deletions

View File

@@ -13,18 +13,29 @@ class ReliefButton: UIButton
internal let backgroundView = GradientView(direction: .vertical, colors: ReliefButton.gradientColors(inverted: false, darkMode: false))
static let padding = CGFloat(24.0)
static let cornerRadius = CGFloat(4.0)
static let borderWidth = CGFloat(1.0)
override var isHighlighted: Bool {
didSet {
get {
super.isHighlighted || remainsPressed
}
set {
super.isHighlighted = newValue
setBackgroundInverted(isHighlighted)
}
}
var remainsPressed: Bool = false {
didSet {
self.isHighlighted = remainsPressed
}
}
init() {
super.init(frame: .zero)
let cornerRadius = CGFloat(4.0)
self.tintColor = .init(dynamicProvider: { traitCollection -> UIColor in
if traitCollection.userInterfaceStyle == .light {
return .init(white: 0.15, alpha: 1.0)
@@ -40,16 +51,16 @@ class ReliefButton: UIButton
shadowView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
shadowView.layer.shadowRadius = 1.0
shadowView.layer.shadowOpacity = 1.0
shadowView.layer.cornerRadius = cornerRadius
shadowView.layer.cornerRadius = Self.cornerRadius
shadowView.layer.masksToBounds = false
shadowView.layer.shouldRasterize = true
shadowView.layer.rasterizationScale = UIScreen.main.scale
addSubview(shadowView)
backgroundView.layer.cornerRadius = cornerRadius
backgroundView.layer.cornerRadius = Self.cornerRadius
backgroundView.isUserInteractionEnabled = false
backgroundView.layer.masksToBounds = true
backgroundView.layer.borderWidth = 1.0
backgroundView.layer.borderWidth = Self.borderWidth
addSubview(backgroundView)
traitCollectionDidChange(nil)
@@ -131,5 +142,9 @@ class ReliefButton: UIButton
backgroundView.frame = CGRect(origin: .zero, size: CGSize(width: backgroundDimension, height: backgroundDimension))
backgroundView.frame = backgroundView.frame.centeredX(inRect: bounds)
shadowView.frame = backgroundView.frame
// Offset by a small amount. Visual illusion caused by the shadow
backgroundView.frame = backgroundView.frame.offsetBy(dx: 0.0, dy: 1.0)
shadowView.frame = shadowView.frame.offsetBy(dx: 0.0, dy: 1.0)
}
}