Skip to content

Instantly share code, notes, and snippets.

@pgpt10
Last active June 14, 2019 09:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgpt10/1004d03faedf191818417d0f5a34353a to your computer and use it in GitHub Desktop.
Save pgpt10/1004d03faedf191818417d0f5a34353a to your computer and use it in GitHub Desktop.
class CustomCollectionViewCell: UICollectionViewCell
{
//Gradient to add in the cell
private lazy var gradient: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.darkGray.cgColor, UIColor.orange.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = self.bounds
return gradientLayer
}()
//Handling adding and removing gradient as selection state changes
override var isSelected: Bool{
didSet{
if self.isSelected
{
self.layer.insertSublayer(self.gradient, at: 0)
}
else
{
self.gradient.removeFromSuperlayer()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment