【问题标题】:Gradient Colors not changing based on Button isSelected State渐变颜色不会根据按钮 isSelected 状态而改变
【发布时间】:2023-03-26 01:50:01
【问题描述】:

我做了一个自定义渐变按钮,这样我就可以在不同的状态下改变渐变颜色:

class GradientButton: UIButton {

public var buttongradient: CAGradientLayer = CAGradientLayer()

override var isSelected: Bool {
    didSet {
        if isSelected {
            buttongradient.colors = [UIColor(hexString: "#ef473a")!, UIColor(hexString: "#cb2d3e")!].map { $0.cgColor }
        } else {
            buttongradient.colors = [UIColor(hexString: "#29a1e2")! , UIColor(hexString: "#485ac8")!].map { $0.cgColor }
        }
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.addthegradientLayer()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.addthegradientLayer()
}

override func layoutSubviews() {
    super.layoutSubviews()
    self.addthegradientLayer()

}

func addthegradientLayer() {
    //the cells gradient colors
    buttongradient.frame = self.bounds
    buttongradient.cornerRadius = buttongradient.frame.height / 2
    buttongradient.colors = [UIColor(hexString: "#29a1e2")! , UIColor(hexString: "#485ac8")!].map { $0.cgColor }
    buttongradient.startPoint = CGPoint(x: 1.0, y: 0.0)
    buttongradient.endPoint = CGPoint(x: 0.0, y: 1.0)
    self.layer.insertSublayer(buttongradient, at: 0)
}

当我运行这段代码时,按钮背景是清晰的。没有渐变显示。

编辑

如 cmets 建议的那样:

buttongradient.frame = self.bounds

是解决方案。

跟进问题

现在我意识到,渐变不会根据 isSelected 状态改变颜色。

【问题讨论】:

标签: ios swift uibutton gradient


【解决方案1】:

有多个问题:

  1. 就像现在一样,您必须使用self.bounds 来更新图层框架。
  2. 无需更改初始化程序中的层。 layoutSubviews 总是在渲染之前调用。
  3. 如果您的目标是在按下按钮时更改颜色,您想使用isHighlighted,而不是isSelected
  4. 每个布局都会重写您的颜色值,请从布局方法中删除 buttongradient.colors
  5. 十六进制颜色定义不需要使用字符串。我建议使用整数,例如0x29a1e2(见How to use hex colour values)。
class GradientButton: UIButton {

    public let buttongradient: CAGradientLayer = CAGradientLayer()

    override var isSelected: Bool {  // or isHighlighted?
        didSet {
            updateGradientColors()
        }
    }

    func updateGradientColors() {
        let colors: [UIColor]

        if isSelected {
           colors = [UIColor(hexString: "#ef473a")!, UIColor(hexString: "#cb2d3e")!]
        } else {
           colors = [UIColor(hexString: "#29a1e2")!, UIColor(hexString: "#485ac8")!]
        }

        buttongradient.colors = colors.map { $0.cgColor }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupGradient()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setupGradient()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.updateGradient()
    }

    func setupGradient() {
        buttongradient.startPoint = CGPoint(x: 1.0, y: 0.0)
        buttongradient.endPoint = CGPoint(x: 0.0, y: 1.0)
        self.layer.insertSublayer(buttongradient, at: 0)

        updateGradientColors()
    }

    func updateGradient() {
        buttongradient.frame = self.bounds
        buttongradient.cornerRadius = buttongradient.frame.height / 2
    }
}

【讨论】:

  • 作为 Swift 新手,从 UIButton 调用此代码的最佳方式是什么?是通过调用 updateGradientColors() 函数吗?我希望能够在按下 UIButton 时更改它的颜色,移动到下一个 UIView 并返回到原始 UIView 并且按钮的颜色是新颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2018-07-17
  • 1970-01-01
  • 2020-09-10
  • 2022-12-17
  • 2015-12-11
  • 2019-04-29
相关资源
最近更新 更多