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

磁铁

我想弄清楚当我长按一个单元格时如何在UICollectionViewCell的indexPath中进行打印。

我该如何在Swift中做到这一点?

我到处都在寻找如何执行此操作的示例。在Swift中找不到一个。

首先,您的视图控制器必须为UIGestureRecognizerDelegate然后在您的viewcontrollerviewDidLoad()方法中将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")
        }
    }

该代码基于此答案的Objective-C版本

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Java中将枚举与值一起使用

如何在Swift 3中将URLSession与代理一起使用

如何在RethinkDB中将getall与orderby一起使用

如何在Python中将if语句与数组一起使用?

如何在Firefox中将`jpm`与webextension一起使用?

如何在Spring中将OrderBy与findAll一起使用

如何在Swift 3中将sin(_:)与FloatingPoint值一起使用

如何在Swift 3中将getopt与命令行参数一起使用?

如何在Swift 4中将键值观察与智能键路径一起使用?

如何在VSCode中将cmder与powershell一起使用?

如何在Airflow中将MySqlOperator与xcom一起使用?

如何在Laravel中将docker与npm一起使用?

如何在TestNG中将@Parameters与ITestContext一起使用?

如何在JavaScript中将ArrayBuffers与DataViews一起使用

如何在Swift中将嵌套的json与结构一起使用

如何在RecyclerView中将DataBinding与LiveData一起使用

如何在React中将PropTypes与Typescript一起使用

如何在mongodb中将查找与$ push一起使用

如何在Rails中将jQuery与Sunspot一起使用?

如何在OSX中将OpenCV与python一起使用?

如何在RSpec中将`allow`与`let`一起使用

如何在awk中将模式与变量一起使用

如何在Primefaces中将contextMenu与ContentFlow一起使用

如何在bash中将`tail`与`timeout`一起使用

如何在Swift中将枚举和switch()与UITableViewController一起使用

如何在 Windows 中将 curl 与 xampp 一起使用?

如何在 Angular 中将 ngFor 与 Observable 一起使用?

如何在 Angular 中将 patchValue 与 FormArray 一起使用?

如何在reactjs中将usestate与对象一起使用?