UICollectionView 中的空白区域指的是不包含任何 cell 的区域。
要监听空白区域被点击,可以添加一个 UITapGestureRecognizer 到 UICollectionView 上,并在手势识别器的回调方法中处理空白区域被点击的事件。
// 创建 UITapGestureRecognizer 对象
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
// 将 UITapGestureRecognizer 添加到 UICollectionView 上
collectionView.addGestureRecognizer(tapGesture)
// 处理空白区域被点击的事件
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
// 获取手势在 UICollectionView 上的位置
let location = gesture.location(in: collectionView)
// 检查是否点击了空白区域
if let indexPath = collectionView.indexPathForItem(at: location) {
// 点击了cell
} else {
print("空白区域被点击了")
}
}