如何在Swift中将UILongPressGestureRecognizer与UICollectionViewCell一起使用?

时间:2015-03-24 19:55:33

标签: ios swift uigesturerecognizer uicollectionviewcell

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

我怎样才能在Swift中做到这一点?

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

7 个答案:

答案 0 :(得分:61)

首先,您的视图控制器需要为UIGestureRecognizerDelegate。然后在viewcontroller的viewDidLoad()方法

中为您的collectionView添加一个UILongPressGestureRecognizer
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版本。

答案 1 :(得分:10)

ztan answer转换为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")
    }
}

答案 2 :(得分:4)

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

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

答案 3 :(得分:3)

我发现的一件事是:

if gestureReconizer.state != UIGestureRecognizerState.Ended {
    return
}
在释放长按之前,

不会放置针,这没关系,但我找到了

if gestureRecognizer.state == UIGestureRecognizerState.Began { }  
整个功能周围的

将阻止多个引脚放置,同时在定时器延迟满足后立即出现引脚。

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

答案 4 :(得分:0)

ztan答案已转换为快速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")
    }
}

答案 5 :(得分:0)

快速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 :(得分:0)

如果objc函数调用导致错误( 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")
    }

 }