How to add tap gesture to UICollectionView , while maintaining cell selection? How to add tap gesture to UICollectionView , while maintaining cell selection? ios ios

How to add tap gesture to UICollectionView , while maintaining cell selection?


Whenever you want to add a gesture recognizer, but not steal the touches from the target view, you should set UIGestureRecognizer.cancelsTouchesInView for your gestureRecognizer instance to false.


Instead of trying to force didSelectItem you can just get the indexPath and/or cell this way:

func tap(sender: UITapGestureRecognizer){    if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {        let cell = self.collectionView?.cellForItem(at: indexPath)        print("you can do something with the cell or index path here")    } else {        print("collection view was tapped")    }}


matomo