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

前端 未结 2 1190
一个人的身影
一个人的身影 2021-01-07 17:56

Task

Add a single tap gesture to UICollectionView, do not get in the way of cell selection.

I want some other taps on the no-cell part of the

2条回答
  •  情书的邮戳
    2021-01-07 18:44

    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")
        }
    }
    

提交回复
热议问题