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

问题描述 投票:21回答:6

我想弄清楚当我长按单元格时如何打印UICollectionViewCell的indexPath。

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

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

ios swift uigesturerecognizer uicollectionviewcell
6个回答
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版本。


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")
    }
}

4
投票

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

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

3
投票

我发现的一件事是:

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

在释放长按之前不会放置针,这没关系,但我找到了

if gestureRecognizer.state == UIGestureRecognizerState.Began { }  

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

另外,上面有一种类型:识别器 - >识别器


1
投票

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")
        }
    }
}

0
投票

ztan answer转换为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")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.