我想弄清楚当我长按一个单元格时如何在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] 删除。
我来说两句