1. 在collectionView绘制cllectionViewCell的代理函数中添加长按识别:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPress(sender:)))
cell.addGestureRecognizer(longPress)
2. 识别长按的位置
func cellLongPress(sender:UILongPressGestureRecognizer) {
//获得点击位置
let touchPoint = sender.location(in: self.collectionView)
//识别是否长按后松开的手势
if (sender.state == UIGestureRecognizerState.ended)
{
let indexPath = self.collectionViewInput.indexPathForItem(at: touchPoint)
if indexPath != nil
{
print("你点击的是第\(indexPath_output!.row + 1)个cell")
}
}
}