配置位置w/ UITapGestureRecognizer?


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 {

由于某种原因,indexPath在这里总是nil。有人看到我代码中的错误了吗?

您需要在集合视图中获取点击位置,而不是在父视图中。

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

相关内容

  • 没有找到相关文章

最新更新