How can I customize the selection state of my UICollectionViewCell subclass?

Hello everyone,

I have a question about collection view. I want to add selection animation like when I press cell, background should change color to gray in my custom collection view cell. I tried some solutions but I couldn't achieve that. I override of isSelected function, when I clicked the cell background, it change him color but when I pull my finger, it still waiting same background color. The thing I want that is when I pull my finger on to cell, background should be look like old version. What kind of solutions I should try it for this thing, I checked internet but I could not find right solution for me.

Thank you

 when I press cell, background should change color to gray

Just do this:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = .gray

.

when I pull my finger on to cell, background should be look like old version.

Could you explain more clearly what you mean?

If you want color to change when you touch, before you release, you have to do it with a touchGesture recogniser:

https://stackoverflow.com/questions/15628133/uitapgesturerecognizer-make-it-work-on-touch-down-not-touch-up/15629234#15629234

Then in didSelect, you confirm the color as described before

How can I customize the selection state of my UICollectionViewCell subclass?
 
 
Q