gpt4 book ai didi

ios - 如何更改 Collection View 单元格或按钮单击时的渐变?

转载 作者:行者123 更新时间:2023-11-30 12:03:46 25 4
gpt4 key购买 nike

我在 UIView 上使用渐变,如下所示:

//the cells gradient colors
cellgradient.frame = cell.imageView.bounds
cellgradient.cornerRadius = cellgradient.frame.height / 2
cellgradient.colors = [UIColor(hexString: "#ef473a")!, UIColor(hexString: "#cb2d3e")!].map { $0.cgColor }
cellgradient.startPoint = CGPoint(x: 0.0, y: 0.5)
cellgradient.endPoint = CGPoint(x: 1.0, y: 0.5)
cellgradient.colors = [UIColor(hexString: "#ef473a")!, UIColor(hexString: "#cb2d3e")!]
cell.imageView.layer.insertSublayer(cellgradient, at: 0)

现在,当我选择单元格或取消选择单元格时,我想更改当前渐变。我该怎么做?

编辑

根据答案,我执行了以下操作:

class CollectionViewCell : UICollectionViewCell
{

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
public var checked = false
public var animated = false
public var cellgradient: CAGradientLayer = CAGradientLayer()

override var bounds : CGRect {
didSet {
self.layoutIfNeeded()
}
}

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

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

而且它有效。现在我只剩下一个问题了:单元格的 ImageView 隐藏在渐变后面。我该如何解决这个问题?

最佳答案

我会通过subclassing UICollectionViewCell来做到这一点。您的自定义单元格类可以有一个属性来引用其渐变层。

在单元格的初始值设定项中,像现在一样将渐变层添加到 ImageView 中。 Then, override the isSelected property's didSet并修改所选或正常状态的渐变图层。

关于ios - 如何更改 Collection View 单元格或按钮单击时的渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com