【问题标题】:How to disable UILongPressGestureRecognizer on UICollectionViewCell after there is a long press?长按后如何禁用 UICollectionViewCell 上的 UILongPressGestureRecognizer?
【发布时间】:2019-12-06 22:02:20
【问题描述】:

目前,我在cellForItemAt 的单元格上有一个带有 UILongPressGestureRecognizer 的集合视图:

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressOnCell))
    cell.addGestureRecognizer(longPress)

当用户按住一个单元格时,它会触发一个函数来显示一个名为cellDeleteAppear() 的菜单。但是,在菜单出现在屏幕上之后,用户可以按住另一个单元格,这将导致菜单再次弹出。

@objc func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
        if sender.state == .began {

            cellDeleteAppear()

            let gestureLocation = sender.location(in: self.trayCollectionView)

            if let indexPath = self.trayCollectionView.indexPathForItem(at: gestureLocation) {

            indexPathForDeletion = indexPath

            trayCollectionView.allowsSelection = false

            } else {
                print("long press error at index path")
            }
        }
    }

我的目标是:当菜单处于活动状态时,用户不应该能够按住另一个单元格来触发菜单弹出。任何帮助表示赞赏!

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uigesturerecognizer


    【解决方案1】:

    那就做吧

    var menuShown = false
    @objc func handleLongPressOnCell(_ sender: UILongPressGestureRecognizer) {
       if sender.state == .began {
          guard !menuShown else { return }
          menuShown = true
    

    当你隐藏它时

    menuShown = false
    

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2019-04-08
      • 2015-05-28
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多