【问题标题】:configuring location w/ UITapGestureRecognizer?使用 UITapGestureRecognizer 配置位置?
【发布时间】:2021-05-11 15:32:23
【问题描述】:
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 总是为零。有人看到我的代码中的错误吗?

【问题讨论】:

  • 试试let indexPath = self.collectionView.indexPathForItem(at: sender.location(in: self.collectionView))
  • @Paulw11 谢谢!

标签: ios swift uitapgesturerecognizer indexpath


【解决方案1】:

您需要在收藏视图中获取点击位置,而不是在超级视图中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多