I'm trying to get indexPath on the cell when it is tapped twice.
I'm passing arguments in Selector like this but it is giving error.
What is the correct format for this ?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let subOptioncell : SubOptionsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: subOptionsCVReuseIdentifier, for: indexPath) as! SubOptionsCollectionViewCell
let imageNamed = "\(customizeOptionSelected[indexPath.row])"
subOptioncell.subOptionsImage.image = UIImage(named: imageNamed)
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped(sender: indexPath)))
tap.numberOfTapsRequired = 2
collectionView.addGestureRecognizer(tap)
return subOptioncell
}
}
func doubleTapped(sender: IndexPath) {
print("Double Tap")
}
First of all you are adding tapGesture to
collectionViewinstead ofsubOptioncell.It should be:
Instead of:
You cannot pass other instance with
selectorofUIGestureRecognizer, the only instance you can pass isUI(Tap)GestureRecognizer. If you want the indexPath of that cell you can try like this. First of all set yourselectorofTapGesturelike this.Now method should be like:
Edit: If you want to show/hide image on cell double tap then you need to handle it using
indexPathof cell, for that first declare one instance ofIndexPathand use it insidecellForItemAt indexPath.Now on
doubleTappedaction ofUITapGestureRecognizerset theselectedIndexPath.