I need to set gradient cell color in CollectionView for different conditions.
The conditions are set in ViewController in didSelectItemAt method:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if cellNumbers[indexPath.item] == currentNumberGuess {
updateNumberLabel()
}
}
I managed to change the color of cells in UICollectionViewCell:
class CollectionViewCell: UICollectionViewCell {
private lazy var gradient: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.systemGreen.cgColor, UIColor.green.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = self.bounds
return gradientLayer
}()
override var isSelected: Bool {
didSet {
if self.isSelected {
self.layer.insertSublayer(self.gradient, at: 0)
label.isHidden = true
} else {
self.gradient.removeFromSuperlayer()
}
}
}
@IBOutlet weak var label: UILabel!
}
But I also need to change color of selected cell depends on cellNumbers[indexPath.item] == currentNumberGuess condition. How I can do that?
