ios - 如何快速双击(而不是单击)集合视图单元格?
问题描述
现在,我在 collectionview 单元格中有一堆消息。我现在单击单元格的代码是
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Which cell: ", indexPath)
}
我如何使它只有在双击而不是单击时才会打印?
解决方案
You can add UITapGestureRecognizer in collection view.
private var doubleTapGesture: UITapGestureRecognizer!
func setUpDoubleTap() {
doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapCollectionView))
doubleTapGesture.numberOfTapsRequired = 2
collectionView.addGestureRecognizer(doubleTapGesture)
doubleTapGesture.delaysTouchesBegan = true
}
Call above method from your viewDidLoad as
override func viewDidLoad() {
super.viewDidLoad()
setUpDoubleTap()
}
Then add Gesture selector method in your class
@objc func didDoubleTapCollectionView() {
let pointInCollectionView = doubleTapGesture.location(in: collectionView)
if let selectedIndexPath = collectionView.indexPathForItem(at: pointInCollectionView) {
let selectedCell = collectionView.cellForItem(at: selectedIndexPath)
// Print double tapped cell's path
print("Which cell: ", selectedIndexPath.row)
print(" double tapped")
}
}
didDoubleTapCollectionView method will call only when you will double tap on collection view cell item.
I hope above example will solve your problem.
推荐阅读
- java - 如何在java项目中使用python
- web2py - 是否有 web2py 助手可以将新行转换为
? - python - Python 除以 10
- c++ - 当进程被分叉时,std::cin 没有阻塞
- mysql - 如何在 MYSQL 中将 MM/YY 转换为 YYYY-MM-DD
- r - 不同列中值比例的条形图
- c++ - 关闭 linux systemd 时向我的应用程序发送多个 sigterm 信号
- c# - 将 XML 文档中的值读入 Int Array c#
- php - 以混合方式分布顺序数组项
- azure - Qna 制造商返回具有高置信度分数的答案,例如不在 KB 中的“abcd”,对于这些值,azure search 也返回 null