【问题标题】:How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift?如何在 Swift 中将 UILongPressGestureRecognizer 与 UICollectionViewCell 一起使用?
【发布时间】:2015-05-28 07:10:32
【问题描述】:

我想知道当我长按一个单元格时如何打印 UICollectionViewCell 的 indexPath。

如何在 Swift 中做到这一点?

我已经到处寻找如何做到这一点的示例;在 Swift 中找不到。

【问题讨论】:

标签: ios swift uigesturerecognizer uicollectionviewcell


【解决方案1】:

首先你的视图控制器需要是UIGestureRecognizerDelegate。然后在 viewcontroller 的 viewDidLoad() 方法中添加一个 UILongPressGestureRecognizer 到你的 collectionView 中

class ViewController: UIViewController, UIGestureRecognizerDelegate {

     override func viewDidLoad() {
         super.viewDidLoad()

        let lpgr = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
         lpgr.minimumPressDuration = 0.5
         lpgr.delaysTouchesBegan = true
         lpgr.delegate = self
         self.collectionView.addGestureRecognizer(lpgr)
    }

长按处理方法:

func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizerState.Ended {
            return
        }

        let p = gestureReconizer.locationInView(self.collectionView)
        let indexPath = self.collectionView.indexPathForItemAtPoint(p)

        if let index = indexPath {
            var cell = self.collectionView.cellForItemAtIndexPath(index)
            // do stuff with your cell, for example print the indexPath
             println(index.row)
        } else {
            println("Could not find index path")
        }
    }

此代码基于 this answer 的 Objective-C 版本。

【讨论】:

  • 我的垂直滚动现在不适用于我的 collectionView :(\
【解决方案2】:

ztan 答案已转换为 swift 3 语法和次要拼写更新:

func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
    if gestureRecognizer.state != UIGestureRecognizerState.ended {
      return
    }

    let p = gestureRecognizer.location(in: collectionView)
    let indexPath = collectionView.indexPathForItem(at: p)

    if let index = indexPath {
      var cell = collectionView.cellForItem(at: index)
      // do stuff with your cell, for example print the indexPath
      print(index.row)
    } else {
      print("Could not find index path")
    }
}

【讨论】:

    【解决方案3】:

    我发现的一件事是:

    if gestureReconizer.state != UIGestureRecognizerState.Ended {
        return
    }
    

    在您松开长按之前不会放置别针,这没关系,但我发现了

    if gestureRecognizer.state == UIGestureRecognizerState.Began { }  
    

    围绕整个功能将防止多个引脚放置,同时在满足定时器延迟时让引脚出现。

    另外,上面有一个错字:Reconizer -> Recognizer

    【讨论】:

    • 您说的完全正确,感觉更像是在您按下时放置图钉而不是等到释放压片才放置图钉的原生地图。这里有完整的 Swift 代码:stackoverflow.com/a/29466391/1359088
    【解决方案4】:

    转换为 swift 3 语法的方法 handleLongProgress 工作正常。我只是想补充一点,lpgr的初始化应该改为:

    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureReconizer:)))
    

    【讨论】:

      【解决方案5】:

      Swift 5 和 Swift 4 表格视图单元格

      override func viewDidLoad() {
          //MARK:- Add Long Gesture
          let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
          longPressGesture.minimumPressDuration = 1.0 // 1 second press
          longPressGesture.allowableMovement = 15 // 15 points
          longPressGesture.delegate = self
          self.tablev.addGestureRecognizer(longPressGesture)
      }
      
      
      //MARK:- Long Press Gesture
      @objc func longPressed(sender: UILongPressGestureRecognizer)
      {
      
          if sender.state == UIGestureRecognizer.State.ended {
              return
          }
          else if sender.state == UIGestureRecognizer.State.began
          {
              let p = sender.location(in: self.tablev)
              let indexPath = self.tablev.indexPathForRow(at: p)
      
              if let index = indexPath {
                  var cell = self.tablev.cellForRow(at: index)
                  // do stuff with your cell, for example print the indexPath
                  print(index.row)
                  print("longpressed Tag: \(index.row)")
              } else {
                  print("Could not find index path")
              }
          }
      }
      

      【讨论】:

        【解决方案6】:

        ztan 答案已转换为 swift 4 语法和次要拼写更新:

        @objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
            guard gestureRecognizer.state != .ended else { return }
        
            let point = gestureRecognizer.location(in: collectionView)
        
            if let indexPath = collectionView.indexPathForItem(at: point), 
               let cell = collectionView.cellForItem(at: indexPath) {
                // do stuff with your cell, for example print the indexPath
                print(indexPath.row)
            } else {
                print("Could not find index path")
            }
        }
        

        【讨论】:

          【解决方案7】:

          如果 objc func 调用导致错误(Swift 5),

          Selector("handleLongPress") 替换为#selector(self. handleLongPress) 这是完整的实现。

          在你的 viewDidLoad() 中,

                  let lpgr = UILongPressGestureRecognizer(target: self, 
                                       action:#selector(self.handleLongPress))
                  lpgr.minimumPressDuration = 1
                  lpgr.delaysTouchesBegan = true
                  lpgr.delegate = self
                  self._mapView.addGestureRecognizer(lpgr)
          

          并在您的视图控制器中实现它,

          @objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
                  if gestureReconizer.state != UIGestureRecognizerState.Ended {
                  return
              }
          
              let p = gestureReconizer.locationInView(self.collectionView)
              let indexPath = self.collectionView.indexPathForItemAtPoint(p)
          
              if let index = indexPath {
                  var cell = self.collectionView.cellForItemAtIndexPath(index)
                  // do stuff with your cell, for example print the indexPath
                   println(index.row)
              } else {
                  println("Could not find index path")
              }
          
           }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多