highlighting uicollectionview cell on tap

I had the exact same problem and the solution is actually much simpler then the one posted above.

In your view controller, add collectionView.delaysContentTouches = false.

And then your other code within the cell was fine as is:

class SlideOutMenuCells: UICollectionViewCell {

    //Setup code...

    override var isHighlighted: Bool {
        didSet {
            if self.isHighlighted {
                backgroundColor = UIColor.green
            } else {
                backgroundColor = UIColor.red
            }
        }
    }
}

But now that annoying delay is gone!


You can try to use UILongPressGestureRecognizer to indicate selection:

override func awakeFromNib() {
    super.awakeFromNib()

    let tapGesture = UILongPressGestureRecognizer(target: self, action: #selector(didTap(recognizer:)))
    tapGesture.minimumPressDuration = 0
    tapGesture.cancelsTouchesInView = false
    addGestureRecognizer(tapGesture)
}

@objc func didTap(recognizer: UILongPressGestureRecognizer) {
    if recognizer.state == .began {
        backgroundColor = .red
    }
    if recognizer.state == .ended {
        backgroundColor = .green
    }
}

or you can make extension for UICollectionViewCell

extension UICollectionViewCell {
func makeSelectionIndicatable() {
    let tapGesture = UILongPressGestureRecognizer(target: self, action: #selector(didTap(recognizer:)))
    tapGesture.minimumPressDuration = 0
    tapGesture.cancelsTouchesInView = false
    addGestureRecognizer(tapGesture)
}

@objc private func didTap(recognizer: UILongPressGestureRecognizer) {
    if recognizer.state == .began {
        backgroundColor = .red
    }
    if recognizer.state == .ended {
        backgroundColor = .green
    }
}

}

and after that for any cell at awakeFromNib() method you just need to add makeSelectionIndicatable()


Here is working code for highlighting UICollectionViewCell on tap (swift 4 | swift 5)

Solution 1

class StoreCollViewCell:UICollectionViewCell{

    override var isSelected: Bool {
        didSet {
            self.contentView.backgroundColor = isSelected ? UIColor.red : UIColor.clear
        }
    }
}

Solution 2

Can't need to do anything in your UICollectionViewCell Class.

class StoreListViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    var previousSelected : IndexPath?
    var currentSelected : Int?

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoreCollViewCell", for: indexPath) as! StoreCollViewCell

        // To set the selected cell background color here
        if currentSelected != nil && currentSelected == indexPath.row
        {
            cell.backgroundColor = UIColor.green
        }else{
            cell.backgroundColor = UIColor.yellow
        }
    
        return cell     
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        // For remove previously selection 
        if previousSelected != nil{
            if let cell = collectionView.cellForItem(at: previousSelected!){
                cell.backgroundColor = UIColor.yellow
            }
        }
        currentSelected = indexPath.row
        previousSelected = indexPath

        // For reload the selected cell
        self.collVwStores.reloadItems(at: [indexPath]) 
    }
}

Output

enter image description here

enter image description here