Signal-iOS/SignalUI/Views/GradientView.swift
Igor Solomennikov 1f3ceac66c Fix GradientView not working in some cases.
GradientView would not work if its colors were passed during initialization
and never changed after. This was caused by the fact that `CAGradientLayer`'s
colors were updated in `GradientView.colors.didSet`, which isn't
called during `init()`.

Also allow access to `GradientView.gradientLayer` so that users of
that class can build more advanced gradients.
2022-04-20 10:23:50 -07:00

42 lines
1.1 KiB
Swift

//
// Copyright (c) 2022 Open Whisper Systems. All rights reserved.
//
public class GradientView: UIView {
public let gradientLayer = CAGradientLayer()
public var colors: [(color: UIColor, location: Double)] {
didSet {
updateColors()
}
}
public convenience init(from fromColor: UIColor, to toColor: UIColor) {
self.init(colors: [
(color: fromColor, location: 0),
(color: toColor, location: 1)
])
}
public required init(colors: [(color: UIColor, location: Double)]) {
self.colors = colors
super.init(frame: .zero)
updateColors()
layer.addSublayer(gradientLayer)
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.frame = self.bounds
}
private func updateColors() {
gradientLayer.colors = colors.map { $0.color.cgColor }
gradientLayer.locations = colors.map { NSNumber(value: $0.location) }
}
}