0
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view
        let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
        tap.numberOfTapsRequired = 2
        view.addGestureRecognizer(tap)
    
    }
    
    @objc func doubleTapped(sender: UITapGestureRecognizer) {
        let indexPath = self.collectionView.indexPathForItem(at: sender.location(in: sender.view))

        if let unwrapped = indexPath {

For some reason, indexPath is always nil here. Does anyone see the bug in my code?

1 Answers1

2

You need to get the tap position in your collection view, not the super view.

Use let indexPath = self.collectionView.indexPathForItem(at: sender.location(in: self.collectionView))

Paulw11
  • 108,386
  • 14
  • 159
  • 186