【发布时间】:2017-08-16 14:13:24
【问题描述】:
我有一个班级,我想使用 Storyboard 为 CAGradientLayer 设置最多 4 个 UIColors。
我正在使用一个数组来跟踪从情节提要中设置的 UIColors。
当用户设置颜色时,我想将其添加到数组中,如果他们将颜色设置为空(这甚至可能吗?),我想将其从数组中删除。
渐变层将有 2 到 4 种颜色。
下面的代码是我想出的,但如果可能的话,我正在寻找更简洁的代码。
class ViewController: UIViewController {
var gradientLayer = CAGradientLayer()
var gradientColors = [UIColor]()
@IBInspectable var color1Value: UIColor?
{
didSet {
self.updateGradientLayer()
}
}
@IBInspectable var color2Value: UIColor? {
didSet {
self.updateGradientLayer()
}
}
@IBInspectable var color3Value: UIColor? {
didSet {
self.updateGradientLayer()
}
}
@IBInspectable var color4Value: UIColor? {
didSet {
self.updateGradientLayer()
}
}
func updateGradientLayer() {
gradientColors.removeAll()
gradientColors.append(color1Value!)
gradientColors.append(color2Value!)
gradientColors.append(color3Value!)
gradientColors.append(color4Value!)
gradientLayer.colors = gradientColors
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
标签: ios arrays swift properties setter