簡體   English   中英

長按后如何在 UICollectionViewCell 上禁用 UILongPressGestureRecognizer?

[英]How to disable UILongPressGestureRecognizer on UICollectionViewCell after there is a long press?

目前,我在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")
            }
        }
    }

我的目標是:當菜單處於活動狀態時,用戶不應該按住另一個單元格來觸發菜單彈出。 任何幫助表示贊賞!

然后做

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

當你隱藏它時

menuShown = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM