Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 渐变颜色不会根据按钮的选定状态而改变_Ios_Swift_Uibutton_Gradient - Fatal编程技术网

Ios 渐变颜色不会根据按钮的选定状态而改变

Ios 渐变颜色不会根据按钮的选定状态而改变,ios,swift,uibutton,gradient,Ios,Swift,Uibutton,Gradient,我制作了一个自定义渐变按钮,这样我就可以在不同的状态下更改渐变颜色: class GradientButton: UIButton { public var buttongradient: CAGradientLayer = CAGradientLayer() override var isSelected: Bool { didSet { if isSelected { buttongradient.colors = [UIColor(hex

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

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)
}
当我运行此代码时,按钮背景是清晰的。没有梯度显示

编辑

正如评论中所建议的那样:

buttongradient.frame = self.bounds
这就是解决办法

后续问题


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

存在多个问题:

  • 现在,必须使用
    self.bounds
    更新层框架
  • 不需要更改初始值设定项中的图层。始终在渲染之前调用
    布局子视图
  • 如果您的目标是在按下按钮时更改颜色,则您希望使用
    iHighlighted
    ,而不是
    iSelected
  • 每个版面都将重写颜色值,从版面方法中删除
    按钮radient.colors
  • 十六进制颜色定义不需要使用字符串。我建议使用整数,例如
    0x29a1e2
    (请参阅)

  • buttongraient.frame=self.frame
    =>
    buttongraient.frame=self.bounds
    似乎更好。这就是解决方案!请参阅等。只有当边界确实发生变化时,才应更新渐变,否则将出现巨大的性能问题。我知道,因为我最近在修一个类似的。您可能还想使用
    ishighlight
    而不是
    isSelected
    。对于Swift来说,使用UIButton调用此代码的最佳方式是什么?是通过调用updateGradientColors()函数实现的吗?我希望能够在按下UIView按钮时更改其颜色,移动到下一个UIView并返回到原始UIView,并且按钮的颜色为新颜色。
    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
        }
    }