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