Search code examples
iosswiftuicollectionviewcell

Recognize when the user touches down on UICollectionView cell


In the Facebook app they have a collection View at the top for stories. When a user taps on a cell there is a slight depression and then back, this begins happening before the user even lifts his finger off the cell.

How could I achieve this effect? For the sake of simplicity I would like to know how to highlight that cell.

Update:

Something I realized is that currently when a user taps down on a cell the text labels in it disappear, how can I know where this is happening so I can animate other things?

update 2:

I was able to get close with the following:

@objc private func cellLongPressHandler(_ sender: UILongPressGestureRecognizer) {

    print("TOUCH DOWN")
    // handle touch down and touch up events separately

    if sender.state == .began {


    } else if  sender.state == .ended {

    }

}

And then:

  cell.addGestureRecognizer(cellLongPressGesture) //new jun 16

The issue however is that when this is added, the collection View is made un-swipable and thus unusable. How can I fix this?


Solution

  • Use these methods: SWIFT 4

    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    
    }