I'm using a gradient on my UIViews like this:
//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)
Now when I select the cell or deselect the cell I want to change the current gradient. How do I do this?
EDIT
Based on the answer I did the following:
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)
}
And it works. Now I only got one problem left: The imageview of the cell is hidden behind the gradient. How do I solve this?